home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / gawk-3.000 / gawk-3 / gawk-3.0.0 / regex.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-15  |  175.3 KB  |  5,561 lines

  1. /* Extended regular expression matching and search library,
  2.    version 0.12.
  3.    (Implements POSIX draft P10003.2/D11.2, except for
  4.    internationalization features.)
  5.  
  6.    Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc.
  7.  
  8.    This program is free software; you can redistribute it and/or modify
  9.    it under the terms of the GNU General Public License as published by
  10.    the Free Software Foundation; either version 2, or (at your option)
  11.    any later version.
  12.  
  13.    This program is distributed in the hope that it will be useful,
  14.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.    GNU General Public License for more details.
  17.  
  18.    You should have received a copy of the GNU General Public License
  19.    along with this program; if not, write to the Free Software
  20.    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA */
  21.  
  22. /* AIX requires this to be the first thing in the file. */
  23. #if defined (_AIX) && !defined (REGEX_MALLOC)
  24.   #pragma alloca
  25. #endif
  26.  
  27. #undef    _GNU_SOURCE
  28. #define _GNU_SOURCE
  29.  
  30. #ifdef HAVE_CONFIG_H
  31. #include <config.h>
  32. #endif
  33.  
  34. #if defined(STDC_HEADERS) && !defined(emacs)
  35. #include <stddef.h>
  36. #else
  37. /* We need this for `regex.h', and perhaps for the Emacs include files.  */
  38. #include <sys/types.h>
  39. #endif
  40.  
  41. /* This is for other GNU distributions with internationalized messages.  */
  42. #if HAVE_LIBINTL_H || defined (_LIBC)
  43. # include <libintl.h>
  44. #else
  45. # define gettext(msgid) (msgid)
  46. #endif
  47.  
  48. #ifndef gettext_noop
  49. /* This define is so xgettext can find the internationalizable
  50.    strings.  */
  51. #define gettext_noop(String) String
  52. #endif
  53.  
  54. /* The `emacs' switch turns on certain matching commands
  55.    that make sense only in Emacs. */
  56. #ifdef emacs
  57.  
  58. #include "lisp.h"
  59. #include "buffer.h"
  60. #include "syntax.h"
  61.  
  62. #else  /* not emacs */
  63.  
  64. /* If we are not linking with Emacs proper,
  65.    we can't use the relocating allocator
  66.    even if config.h says that we can.  */
  67. #undef REL_ALLOC
  68.  
  69. #if defined (STDC_HEADERS) || defined (_LIBC)
  70. #include <stdlib.h>
  71. #else
  72. char *malloc ();
  73. char *realloc ();
  74. #endif
  75.  
  76. /* When used in Emacs's lib-src, we need to get bzero and bcopy somehow.
  77.    If nothing else has been done, use the method below.  */
  78. #ifdef INHIBIT_STRING_HEADER
  79. #if !(defined (HAVE_BZERO) && defined (HAVE_BCOPY))
  80. #if !defined (bzero) && !defined (bcopy)
  81. #undef INHIBIT_STRING_HEADER
  82. #endif
  83. #endif
  84. #endif
  85.  
  86. /* This is the normal way of making sure we have a bcopy and a bzero.
  87.    This is used in most programs--a few other programs avoid this
  88.    by defining INHIBIT_STRING_HEADER.  */
  89. #ifndef INHIBIT_STRING_HEADER
  90. #if defined (HAVE_STRING_H) || defined (STDC_HEADERS) || defined (_LIBC)
  91. #include <string.h>
  92. #ifndef bcmp
  93. #define bcmp(s1, s2, n)    memcmp ((s1), (s2), (n))
  94. #endif
  95. #ifndef bcopy
  96. #define bcopy(s, d, n)    memcpy ((d), (s), (n))
  97. #endif
  98. #ifndef bzero
  99. #define bzero(s, n)    memset ((s), 0, (n))
  100. #endif
  101. #else
  102. #include <strings.h>
  103. #endif
  104. #endif
  105.  
  106. /* Define the syntax stuff for \<, \>, etc.  */
  107.  
  108. /* This must be nonzero for the wordchar and notwordchar pattern
  109.    commands in re_match_2.  */
  110. #ifndef Sword
  111. #define Sword 1
  112. #endif
  113.  
  114. #ifdef SWITCH_ENUM_BUG
  115. #define SWITCH_ENUM_CAST(x) ((int)(x))
  116. #else
  117. #define SWITCH_ENUM_CAST(x) (x)
  118. #endif
  119.  
  120. #ifdef SYNTAX_TABLE
  121.  
  122. extern char *re_syntax_table;
  123.  
  124. #else /* not SYNTAX_TABLE */
  125.  
  126. /* How many characters in the character set.  */
  127. #define CHAR_SET_SIZE 256
  128.  
  129. static char re_syntax_table[CHAR_SET_SIZE];
  130.  
  131. static void
  132. init_syntax_once ()
  133. {
  134.    register int c;
  135.    static int done = 0;
  136.  
  137.    if (done)
  138.      return;
  139.  
  140.    bzero (re_syntax_table, sizeof re_syntax_table);
  141.  
  142.    for (c = 'a'; c <= 'z'; c++)
  143.      re_syntax_table[c] = Sword;
  144.  
  145.    for (c = 'A'; c <= 'Z'; c++)
  146.      re_syntax_table[c] = Sword;
  147.  
  148.    for (c = '0'; c <= '9'; c++)
  149.      re_syntax_table[c] = Sword;
  150.  
  151.    re_syntax_table['_'] = Sword;
  152.  
  153.    done = 1;
  154. }
  155.  
  156. #endif /* not SYNTAX_TABLE */
  157.  
  158. #define SYNTAX(c) re_syntax_table[c]
  159.  
  160. #endif /* not emacs */
  161.  
  162. /* Get the interface, including the syntax bits.  */
  163. #include "regex.h"
  164.  
  165. /* isalpha etc. are used for the character classes.  */
  166. #include <ctype.h>
  167.  
  168. /* Jim Meyering writes:
  169.  
  170.    "... Some ctype macros are valid only for character codes that
  171.    isascii says are ASCII (SGI's IRIX-4.0.5 is one such system --when
  172.    using /bin/cc or gcc but without giving an ansi option).  So, all
  173.    ctype uses should be through macros like ISPRINT...  If
  174.    STDC_HEADERS is defined, then autoconf has verified that the ctype
  175.    macros don't need to be guarded with references to isascii. ...
  176.    Defining isascii to 1 should let any compiler worth its salt
  177.    eliminate the && through constant folding."  */
  178.  
  179. #if defined (STDC_HEADERS) || (!defined (isascii) && !defined (HAVE_ISASCII))
  180. #define ISASCII(c) 1
  181. #else
  182. #define ISASCII(c) isascii(c)
  183. #endif
  184.  
  185. #ifdef isblank
  186. #define ISBLANK(c) (ISASCII (c) && isblank (c))
  187. #else
  188. #define ISBLANK(c) ((c) == ' ' || (c) == '\t')
  189. #endif
  190. #ifdef isgraph
  191. #define ISGRAPH(c) (ISASCII (c) && isgraph (c))
  192. #else
  193. #define ISGRAPH(c) (ISASCII (c) && isprint (c) && !isspace (c))
  194. #endif
  195.  
  196. #define ISPRINT(c) (ISASCII (c) && isprint (c))
  197. #define ISDIGIT(c) (ISASCII (c) && isdigit (c))
  198. #define ISALNUM(c) (ISASCII (c) && isalnum (c))
  199. #define ISALPHA(c) (ISASCII (c) && isalpha (c))
  200. #define ISCNTRL(c) (ISASCII (c) && iscntrl (c))
  201. #define ISLOWER(c) (ISASCII (c) && islower (c))
  202. #define ISPUNCT(c) (ISASCII (c) && ispunct (c))
  203. #define ISSPACE(c) (ISASCII (c) && isspace (c))
  204. #define ISUPPER(c) (ISASCII (c) && isupper (c))
  205. #define ISXDIGIT(c) (ISASCII (c) && isxdigit (c))
  206.  
  207. #ifndef NULL
  208. #define NULL (void *)0
  209. #endif
  210.  
  211. /* We remove any previous definition of `SIGN_EXTEND_CHAR',
  212.    since ours (we hope) works properly with all combinations of
  213.    machines, compilers, `char' and `unsigned char' argument types.
  214.    (Per Bothner suggested the basic approach.)  */
  215. #undef SIGN_EXTEND_CHAR
  216. #if __STDC__
  217. #define SIGN_EXTEND_CHAR(c) ((signed char) (c))
  218. #else  /* not __STDC__ */
  219. /* As in Harbison and Steele.  */
  220. #define SIGN_EXTEND_CHAR(c) ((((unsigned char) (c)) ^ 128) - 128)
  221. #endif
  222.  
  223. /* Should we use malloc or alloca?  If REGEX_MALLOC is not defined, we
  224.    use `alloca' instead of `malloc'.  This is because using malloc in
  225.    re_search* or re_match* could cause memory leaks when C-g is used in
  226.    Emacs; also, malloc is slower and causes storage fragmentation.  On
  227.    the other hand, malloc is more portable, and easier to debug.
  228.  
  229.    Because we sometimes use alloca, some routines have to be macros,
  230.    not functions -- `alloca'-allocated space disappears at the end of the
  231.    function it is called in.  */
  232.  
  233. #ifdef REGEX_MALLOC
  234.  
  235. #define REGEX_ALLOCATE malloc
  236. #define REGEX_REALLOCATE(source, osize, nsize) realloc (source, nsize)
  237. #define REGEX_FREE free
  238.  
  239. #else /* not REGEX_MALLOC  */
  240.  
  241. /* Emacs already defines alloca, sometimes.  */
  242. #ifndef alloca
  243.  
  244. /* Make alloca work the best possible way.  */
  245. #ifdef __GNUC__
  246. #define alloca __builtin_alloca
  247. #else /* not __GNUC__ */
  248. #if HAVE_ALLOCA_H
  249. #include <alloca.h>
  250. #else /* not __GNUC__ or HAVE_ALLOCA_H */
  251. #if 0 /* It is a bad idea to declare alloca.  We always cast the result.  */
  252. #ifndef _AIX /* Already did AIX, up at the top.  */
  253. char *alloca ();
  254. #endif /* not _AIX */
  255. #endif
  256. #endif /* not HAVE_ALLOCA_H */
  257. #endif /* not __GNUC__ */
  258.  
  259. #endif /* not alloca */
  260.  
  261. #define REGEX_ALLOCATE alloca
  262.  
  263. /* Assumes a `char *destination' variable.  */
  264. #define REGEX_REALLOCATE(source, osize, nsize)                \
  265.   (destination = (char *) alloca (nsize),                \
  266.    bcopy (source, destination, osize),                    \
  267.    destination)
  268.  
  269. /* No need to do anything to free, after alloca.  */
  270. #define REGEX_FREE(arg) ((void)0) /* Do nothing!  But inhibit gcc warning.  */
  271.  
  272. #endif /* not REGEX_MALLOC */
  273.  
  274. /* Define how to allocate the failure stack.  */
  275.  
  276. #if defined (REL_ALLOC) && defined (REGEX_MALLOC)
  277.  
  278. #define REGEX_ALLOCATE_STACK(size)                \
  279.   r_alloc (&failure_stack_ptr, (size))
  280. #define REGEX_REALLOCATE_STACK(source, osize, nsize)        \
  281.   r_re_alloc (&failure_stack_ptr, (nsize))
  282. #define REGEX_FREE_STACK(ptr)                    \
  283.   r_alloc_free (&failure_stack_ptr)
  284.  
  285. #else /* not using relocating allocator */
  286.  
  287. #ifdef REGEX_MALLOC
  288.  
  289. #define REGEX_ALLOCATE_STACK malloc
  290. #define REGEX_REALLOCATE_STACK(source, osize, nsize) realloc (source, nsize)
  291. #define REGEX_FREE_STACK free
  292.  
  293. #else /* not REGEX_MALLOC */
  294.  
  295. #define REGEX_ALLOCATE_STACK alloca
  296.  
  297. #define REGEX_REALLOCATE_STACK(source, osize, nsize)            \
  298.    REGEX_REALLOCATE (source, osize, nsize)
  299. /* No need to explicitly free anything.  */
  300. #define REGEX_FREE_STACK(arg)
  301.  
  302. #endif /* not REGEX_MALLOC */
  303. #endif /* not using relocating allocator */
  304.  
  305.  
  306. /* True if `size1' is non-NULL and PTR is pointing anywhere inside
  307.    `string1' or just past its end.  This works if PTR is NULL, which is
  308.    a good thing.  */
  309. #define FIRST_STRING_P(ptr)                     \
  310.   (size1 && string1 <= (ptr) && (ptr) <= string1 + size1)
  311.  
  312. /* (Re)Allocate N items of type T using malloc, or fail.  */
  313. #define TALLOC(n, t) ((t *) malloc ((n) * sizeof (t)))
  314. #define RETALLOC(addr, n, t) ((addr) = (t *) realloc (addr, (n) * sizeof (t)))
  315. #define RETALLOC_IF(addr, n, t) \
  316.   if (addr) RETALLOC((addr), (n), t); else (addr) = TALLOC ((n), t)
  317. #define REGEX_TALLOC(n, t) ((t *) REGEX_ALLOCATE ((n) * sizeof (t)))
  318.  
  319. #define BYTEWIDTH 8 /* In bits.  */
  320.  
  321. #define STREQ(s1, s2) ((strcmp (s1, s2) == 0))
  322.  
  323. #undef MAX
  324. #undef MIN
  325. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  326. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  327.  
  328. typedef char boolean;
  329. #define false 0
  330. #define true 1
  331.  
  332. static int re_match_2_internal ();
  333.  
  334. /* These are the command codes that appear in compiled regular
  335.    expressions.  Some opcodes are followed by argument bytes.  A
  336.    command code can specify any interpretation whatsoever for its
  337.    arguments.  Zero bytes may appear in the compiled regular expression.  */
  338.  
  339. typedef enum
  340. {
  341.   no_op = 0,
  342.  
  343.   /* Succeed right away--no more backtracking.  */
  344.   succeed,
  345.  
  346.         /* Followed by one byte giving n, then by n literal bytes.  */
  347.   exactn,
  348.  
  349.         /* Matches any (more or less) character.  */
  350.   anychar,
  351.  
  352.         /* Matches any one char belonging to specified set.  First
  353.            following byte is number of bitmap bytes.  Then come bytes
  354.            for a bitmap saying which chars are in.  Bits in each byte
  355.            are ordered low-bit-first.  A character is in the set if its
  356.            bit is 1.  A character too large to have a bit in the map is
  357.            automatically not in the set.  */
  358.   charset,
  359.  
  360.         /* Same parameters as charset, but match any character that is
  361.            not one of those specified.  */
  362.   charset_not,
  363.  
  364.         /* Start remembering the text that is matched, for storing in a
  365.            register.  Followed by one byte with the register number, in
  366.            the range 0 to one less than the pattern buffer's re_nsub
  367.            field.  Then followed by one byte with the number of groups
  368.            inner to this one.  (This last has to be part of the
  369.            start_memory only because we need it in the on_failure_jump
  370.            of re_match_2.)  */
  371.   start_memory,
  372.  
  373.         /* Stop remembering the text that is matched and store it in a
  374.            memory register.  Followed by one byte with the register
  375.            number, in the range 0 to one less than `re_nsub' in the
  376.            pattern buffer, and one byte with the number of inner groups,
  377.            just like `start_memory'.  (We need the number of inner
  378.            groups here because we don't have any easy way of finding the
  379.            corresponding start_memory when we're at a stop_memory.)  */
  380.   stop_memory,
  381.  
  382.         /* Match a duplicate of something remembered. Followed by one
  383.            byte containing the register number.  */
  384.   duplicate,
  385.  
  386.         /* Fail unless at beginning of line.  */
  387.   begline,
  388.  
  389.         /* Fail unless at end of line.  */
  390.   endline,
  391.  
  392.         /* Succeeds if at beginning of buffer (if emacs) or at beginning
  393.            of string to be matched (if not).  */
  394.   begbuf,
  395.  
  396.         /* Analogously, for end of buffer/string.  */
  397.   endbuf,
  398.  
  399.         /* Followed by two byte relative address to which to jump.  */
  400.   jump,
  401.  
  402.     /* Same as jump, but marks the end of an alternative.  */
  403.   jump_past_alt,
  404.  
  405.         /* Followed by two-byte relative address of place to resume at
  406.            in case of failure.  */
  407.   on_failure_jump,
  408.  
  409.         /* Like on_failure_jump, but pushes a placeholder instead of the
  410.            current string position when executed.  */
  411.   on_failure_keep_string_jump,
  412.  
  413.         /* Throw away latest failure point and then jump to following
  414.            two-byte relative address.  */
  415.   pop_failure_jump,
  416.  
  417.         /* Change to pop_failure_jump if know won't have to backtrack to
  418.            match; otherwise change to jump.  This is used to jump
  419.            back to the beginning of a repeat.  If what follows this jump
  420.            clearly won't match what the repeat does, such that we can be
  421.            sure that there is no use backtracking out of repetitions
  422.            already matched, then we change it to a pop_failure_jump.
  423.            Followed by two-byte address.  */
  424.   maybe_pop_jump,
  425.  
  426.         /* Jump to following two-byte address, and push a dummy failure
  427.            point. This failure point will be thrown away if an attempt
  428.            is made to use it for a failure.  A `+' construct makes this
  429.            before the first repeat.  Also used as an intermediary kind
  430.            of jump when compiling an alternative.  */
  431.   dummy_failure_jump,
  432.  
  433.     /* Push a dummy failure point and continue.  Used at the end of
  434.        alternatives.  */
  435.   push_dummy_failure,
  436.  
  437.         /* Followed by two-byte relative address and two-byte number n.
  438.            After matching N times, jump to the address upon failure.  */
  439.   succeed_n,
  440.  
  441.         /* Followed by two-byte relative address, and two-byte number n.
  442.            Jump to the address N times, then fail.  */
  443.   jump_n,
  444.  
  445.         /* Set the following two-byte relative address to the
  446.            subsequent two-byte number.  The address *includes* the two
  447.            bytes of number.  */
  448.   set_number_at,
  449.  
  450.   wordchar,    /* Matches any word-constituent character.  */
  451.   notwordchar,    /* Matches any char that is not a word-constituent.  */
  452.  
  453.   wordbeg,    /* Succeeds if at word beginning.  */
  454.   wordend,    /* Succeeds if at word end.  */
  455.  
  456.   wordbound,    /* Succeeds if at a word boundary.  */
  457.   notwordbound    /* Succeeds if not at a word boundary.  */
  458.  
  459. #ifdef emacs
  460.   ,before_dot,    /* Succeeds if before point.  */
  461.   at_dot,    /* Succeeds if at point.  */
  462.   after_dot,    /* Succeeds if after point.  */
  463.  
  464.     /* Matches any character whose syntax is specified.  Followed by
  465.            a byte which contains a syntax code, e.g., Sword.  */
  466.   syntaxspec,
  467.  
  468.     /* Matches any character whose syntax is not that specified.  */
  469.   notsyntaxspec
  470. #endif /* emacs */
  471. } re_opcode_t;
  472.  
  473. /* Common operations on the compiled pattern.  */
  474.  
  475. /* Store NUMBER in two contiguous bytes starting at DESTINATION.  */
  476.  
  477. #define STORE_NUMBER(destination, number)                \
  478.   do {                                    \
  479.     (destination)[0] = (number) & 0377;                    \
  480.     (destination)[1] = (number) >> 8;                    \
  481.   } while (0)
  482.  
  483. /* Same as STORE_NUMBER, except increment DESTINATION to
  484.    the byte after where the number is stored.  Therefore, DESTINATION
  485.    must be an lvalue.  */
  486.  
  487. #define STORE_NUMBER_AND_INCR(destination, number)            \
  488.   do {                                    \
  489.     STORE_NUMBER (destination, number);                    \
  490.     (destination) += 2;                            \
  491.   } while (0)
  492.  
  493. /* Put into DESTINATION a number stored in two contiguous bytes starting
  494.    at SOURCE.  */
  495.  
  496. #define EXTRACT_NUMBER(destination, source)                \
  497.   do {                                    \
  498.     (destination) = *(source) & 0377;                    \
  499.     (destination) += SIGN_EXTEND_CHAR (*((source) + 1)) << 8;        \
  500.   } while (0)
  501.  
  502. #ifdef DEBUG
  503. static void extract_number _RE_ARGS((int *dest, unsigned char *source));
  504. static void
  505. extract_number (dest, source)
  506.     int *dest;
  507.     unsigned char *source;
  508. {
  509.   int temp = SIGN_EXTEND_CHAR (*(source + 1));
  510.   *dest = *source & 0377;
  511.   *dest += temp << 8;
  512. }
  513.  
  514. #ifndef EXTRACT_MACROS /* To debug the macros.  */
  515. #undef EXTRACT_NUMBER
  516. #define EXTRACT_NUMBER(dest, src) extract_number (&dest, src)
  517. #endif /* not EXTRACT_MACROS */
  518.  
  519. #endif /* DEBUG */
  520.  
  521. /* Same as EXTRACT_NUMBER, except increment SOURCE to after the number.
  522.    SOURCE must be an lvalue.  */
  523.  
  524. #define EXTRACT_NUMBER_AND_INCR(destination, source)            \
  525.   do {                                    \
  526.     EXTRACT_NUMBER (destination, source);                \
  527.     (source) += 2;                             \
  528.   } while (0)
  529.  
  530. #ifdef DEBUG
  531. static void extract_number_and_incr _RE_ARGS((int *destination,
  532.                        unsigned char **source));
  533. static void
  534. extract_number_and_incr (destination, source)
  535.     int *destination;
  536.     unsigned char **source;
  537. {
  538.   extract_number (destination, *source);
  539.   *source += 2;
  540. }
  541.  
  542. #ifndef EXTRACT_MACROS
  543. #undef EXTRACT_NUMBER_AND_INCR
  544. #define EXTRACT_NUMBER_AND_INCR(dest, src) \
  545.   extract_number_and_incr (&dest, &src)
  546. #endif /* not EXTRACT_MACROS */
  547.  
  548. #endif /* DEBUG */
  549.  
  550. /* If DEBUG is defined, Regex prints many voluminous messages about what
  551.    it is doing (if the variable `debug' is nonzero).  If linked with the
  552.    main program in `iregex.c', you can enter patterns and strings
  553.    interactively.  And if linked with the main program in `main.c' and
  554.    the other test files, you can run the already-written tests.  */
  555.  
  556. #ifdef DEBUG
  557.  
  558. /* We use standard I/O for debugging.  */
  559. #include <stdio.h>
  560.  
  561. /* It is useful to test things that ``must'' be true when debugging.  */
  562. #include <assert.h>
  563.  
  564. static int debug = 0;
  565.  
  566. #define DEBUG_STATEMENT(e) e
  567. #define DEBUG_PRINT1(x) if (debug) printf (x)
  568. #define DEBUG_PRINT2(x1, x2) if (debug) printf (x1, x2)
  569. #define DEBUG_PRINT3(x1, x2, x3) if (debug) printf (x1, x2, x3)
  570. #define DEBUG_PRINT4(x1, x2, x3, x4) if (debug) printf (x1, x2, x3, x4)
  571. #define DEBUG_PRINT_COMPILED_PATTERN(p, s, e)                 \
  572.   if (debug) print_partial_compiled_pattern (s, e)
  573. #define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2)            \
  574.   if (debug) print_double_string (w, s1, sz1, s2, sz2)
  575.  
  576.  
  577. /* Print the fastmap in human-readable form.  */
  578.  
  579. void
  580. print_fastmap (fastmap)
  581.     char *fastmap;
  582. {
  583.   unsigned was_a_range = 0;
  584.   unsigned i = 0;
  585.  
  586.   while (i < (1 << BYTEWIDTH))
  587.     {
  588.       if (fastmap[i++])
  589.     {
  590.       was_a_range = 0;
  591.           putchar (i - 1);
  592.           while (i < (1 << BYTEWIDTH)  &&  fastmap[i])
  593.             {
  594.               was_a_range = 1;
  595.               i++;
  596.             }
  597.       if (was_a_range)
  598.             {
  599.               printf ("-");
  600.               putchar (i - 1);
  601.             }
  602.         }
  603.     }
  604.   putchar ('\n');
  605. }
  606.  
  607.  
  608. /* Print a compiled pattern string in human-readable form, starting at
  609.    the START pointer into it and ending just before the pointer END.  */
  610.  
  611. void
  612. print_partial_compiled_pattern (start, end)
  613.     unsigned char *start;
  614.     unsigned char *end;
  615. {
  616.   int mcnt, mcnt2;
  617.   unsigned char *p = start;
  618.   unsigned char *pend = end;
  619.  
  620.   if (start == NULL)
  621.     {
  622.       printf ("(null)\n");
  623.       return;
  624.     }
  625.  
  626.   /* Loop over pattern commands.  */
  627.   while (p < pend)
  628.     {
  629.       printf ("%d:\t", p - start);
  630.  
  631.       switch ((re_opcode_t) *p++)
  632.     {
  633.         case no_op:
  634.           printf ("/no_op");
  635.           break;
  636.  
  637.     case exactn:
  638.       mcnt = *p++;
  639.           printf ("/exactn/%d", mcnt);
  640.           do
  641.         {
  642.               putchar ('/');
  643.           putchar (*p++);
  644.             }
  645.           while (--mcnt);
  646.           break;
  647.  
  648.     case start_memory:
  649.           mcnt = *p++;
  650.           printf ("/start_memory/%d/%d", mcnt, *p++);
  651.           break;
  652.  
  653.     case stop_memory:
  654.           mcnt = *p++;
  655.       printf ("/stop_memory/%d/%d", mcnt, *p++);
  656.           break;
  657.  
  658.     case duplicate:
  659.       printf ("/duplicate/%d", *p++);
  660.       break;
  661.  
  662.     case anychar:
  663.       printf ("/anychar");
  664.       break;
  665.  
  666.     case charset:
  667.         case charset_not:
  668.           {
  669.             register int c, last = -100;
  670.         register int in_range = 0;
  671.  
  672.         printf ("/charset [%s",
  673.                 (re_opcode_t) *(p - 1) == charset_not ? "^" : "");
  674.  
  675.             assert (p + *p < pend);
  676.  
  677.             for (c = 0; c < 256; c++)
  678.           if (c / 8 < *p
  679.           && (p[1 + (c/8)] & (1 << (c % 8))))
  680.         {
  681.           /* Are we starting a range?  */
  682.           if (last + 1 == c && ! in_range)
  683.             {
  684.               putchar ('-');
  685.               in_range = 1;
  686.             }
  687.           /* Have we broken a range?  */
  688.           else if (last + 1 != c && in_range)
  689.               {
  690.               putchar (last);
  691.               in_range = 0;
  692.             }
  693.  
  694.           if (! in_range)
  695.             putchar (c);
  696.  
  697.           last = c;
  698.               }
  699.  
  700.         if (in_range)
  701.           putchar (last);
  702.  
  703.         putchar (']');
  704.  
  705.         p += 1 + *p;
  706.       }
  707.       break;
  708.  
  709.     case begline:
  710.       printf ("/begline");
  711.           break;
  712.  
  713.     case endline:
  714.           printf ("/endline");
  715.           break;
  716.  
  717.     case on_failure_jump:
  718.           extract_number_and_incr (&mcnt, &p);
  719.         printf ("/on_failure_jump to %d", p + mcnt - start);
  720.           break;
  721.  
  722.     case on_failure_keep_string_jump:
  723.           extract_number_and_incr (&mcnt, &p);
  724.         printf ("/on_failure_keep_string_jump to %d", p + mcnt - start);
  725.           break;
  726.  
  727.     case dummy_failure_jump:
  728.           extract_number_and_incr (&mcnt, &p);
  729.         printf ("/dummy_failure_jump to %d", p + mcnt - start);
  730.           break;
  731.  
  732.     case push_dummy_failure:
  733.           printf ("/push_dummy_failure");
  734.           break;
  735.  
  736.         case maybe_pop_jump:
  737.           extract_number_and_incr (&mcnt, &p);
  738.         printf ("/maybe_pop_jump to %d", p + mcnt - start);
  739.       break;
  740.  
  741.         case pop_failure_jump:
  742.       extract_number_and_incr (&mcnt, &p);
  743.         printf ("/pop_failure_jump to %d", p + mcnt - start);
  744.       break;
  745.  
  746.         case jump_past_alt:
  747.       extract_number_and_incr (&mcnt, &p);
  748.         printf ("/jump_past_alt to %d", p + mcnt - start);
  749.       break;
  750.  
  751.         case jump:
  752.       extract_number_and_incr (&mcnt, &p);
  753.         printf ("/jump to %d", p + mcnt - start);
  754.       break;
  755.  
  756.         case succeed_n:
  757.           extract_number_and_incr (&mcnt, &p);
  758.           extract_number_and_incr (&mcnt2, &p);
  759.       printf ("/succeed_n to %d, %d times", p + mcnt - start, mcnt2);
  760.           break;
  761.  
  762.         case jump_n:
  763.           extract_number_and_incr (&mcnt, &p);
  764.           extract_number_and_incr (&mcnt2, &p);
  765.       printf ("/jump_n to %d, %d times", p + mcnt - start, mcnt2);
  766.           break;
  767.  
  768.         case set_number_at:
  769.           extract_number_and_incr (&mcnt, &p);
  770.           extract_number_and_incr (&mcnt2, &p);
  771.       printf ("/set_number_at location %d to %d", p + mcnt - start, mcnt2);
  772.           break;
  773.  
  774.         case wordbound:
  775.       printf ("/wordbound");
  776.       break;
  777.  
  778.     case notwordbound:
  779.       printf ("/notwordbound");
  780.           break;
  781.  
  782.     case wordbeg:
  783.       printf ("/wordbeg");
  784.       break;
  785.  
  786.     case wordend:
  787.       printf ("/wordend");
  788.  
  789. #ifdef emacs
  790.     case before_dot:
  791.       printf ("/before_dot");
  792.           break;
  793.  
  794.     case at_dot:
  795.       printf ("/at_dot");
  796.           break;
  797.  
  798.     case after_dot:
  799.       printf ("/after_dot");
  800.           break;
  801.  
  802.     case syntaxspec:
  803.           printf ("/syntaxspec");
  804.       mcnt = *p++;
  805.       printf ("/%d", mcnt);
  806.           break;
  807.  
  808.     case notsyntaxspec:
  809.           printf ("/notsyntaxspec");
  810.       mcnt = *p++;
  811.       printf ("/%d", mcnt);
  812.       break;
  813. #endif /* emacs */
  814.  
  815.     case wordchar:
  816.       printf ("/wordchar");
  817.           break;
  818.  
  819.     case notwordchar:
  820.       printf ("/notwordchar");
  821.           break;
  822.  
  823.     case begbuf:
  824.       printf ("/begbuf");
  825.           break;
  826.  
  827.     case endbuf:
  828.       printf ("/endbuf");
  829.           break;
  830.  
  831.         default:
  832.           printf ("?%d", *(p-1));
  833.     }
  834.  
  835.       putchar ('\n');
  836.     }
  837.  
  838.   printf ("%d:\tend of pattern.\n", p - start);
  839. }
  840.  
  841.  
  842. void
  843. print_compiled_pattern (bufp)
  844.     struct re_pattern_buffer *bufp;
  845. {
  846.   unsigned char *buffer = bufp->buffer;
  847.  
  848.   print_partial_compiled_pattern (buffer, buffer + bufp->used);
  849.   printf ("%d bytes used/%d bytes allocated.\n", bufp->used, bufp->allocated);
  850.  
  851.   if (bufp->fastmap_accurate && bufp->fastmap)
  852.     {
  853.       printf ("fastmap: ");
  854.       print_fastmap (bufp->fastmap);
  855.     }
  856.  
  857.   printf ("re_nsub: %d\t", bufp->re_nsub);
  858.   printf ("regs_alloc: %d\t", bufp->regs_allocated);
  859.   printf ("can_be_null: %d\t", bufp->can_be_null);
  860.   printf ("newline_anchor: %d\n", bufp->newline_anchor);
  861.   printf ("no_sub: %d\t", bufp->no_sub);
  862.   printf ("not_bol: %d\t", bufp->not_bol);
  863.   printf ("not_eol: %d\t", bufp->not_eol);
  864.   printf ("syntax: %d\n", bufp->syntax);
  865.   /* Perhaps we should print the translate table?  */
  866. }
  867.  
  868.  
  869. void
  870. print_double_string (where, string1, size1, string2, size2)
  871.     const char *where;
  872.     const char *string1;
  873.     const char *string2;
  874.     int size1;
  875.     int size2;
  876. {
  877.   unsigned this_char;
  878.  
  879.   if (where == NULL)
  880.     printf ("(null)");
  881.   else
  882.     {
  883.       if (FIRST_STRING_P (where))
  884.         {
  885.           for (this_char = where - string1; this_char < size1; this_char++)
  886.             putchar (string1[this_char]);
  887.  
  888.           where = string2;
  889.         }
  890.  
  891.       for (this_char = where - string2; this_char < size2; this_char++)
  892.         putchar (string2[this_char]);
  893.     }
  894. }
  895.  
  896. void
  897. printchar (c)
  898.     int c;
  899. {
  900.     putc(c, stderr);
  901. }
  902.  
  903. #else /* not DEBUG */
  904.  
  905. #undef assert
  906. #define assert(e)
  907.  
  908. #define DEBUG_STATEMENT(e)
  909. #define DEBUG_PRINT1(x)
  910. #define DEBUG_PRINT2(x1, x2)
  911. #define DEBUG_PRINT3(x1, x2, x3)
  912. #define DEBUG_PRINT4(x1, x2, x3, x4)
  913. #define DEBUG_PRINT_COMPILED_PATTERN(p, s, e)
  914. #define DEBUG_PRINT_DOUBLE_STRING(w, s1, sz1, s2, sz2)
  915.  
  916. #endif /* not DEBUG */
  917.  
  918. /* Set by `re_set_syntax' to the current regexp syntax to recognize.  Can
  919.    also be assigned to arbitrarily: each pattern buffer stores its own
  920.    syntax, so it can be changed between regex compilations.  */
  921. /* This has no initializer because initialized variables in Emacs
  922.    become read-only after dumping.  */
  923. reg_syntax_t re_syntax_options;
  924.  
  925.  
  926. /* Specify the precise syntax of regexps for compilation.  This provides
  927.    for compatibility for various utilities which historically have
  928.    different, incompatible syntaxes.
  929.  
  930.    The argument SYNTAX is a bit mask comprised of the various bits
  931.    defined in regex.h.  We return the old syntax.  */
  932.  
  933. reg_syntax_t
  934. re_set_syntax (syntax)
  935.     reg_syntax_t syntax;
  936. {
  937.   reg_syntax_t ret = re_syntax_options;
  938.  
  939.   re_syntax_options = syntax;
  940.   return ret;
  941. }
  942.  
  943. /* This table gives an error message for each of the error codes listed
  944.    in regex.h.  Obviously the order here has to be same as there.
  945.    POSIX doesn't require that we do anything for REG_NOERROR,
  946.    but why not be nice?  */
  947.  
  948. static const char *re_error_msgid[] =
  949.   {
  950.     gettext_noop ("Success"),    /* REG_NOERROR */
  951.     gettext_noop ("No match"),    /* REG_NOMATCH */
  952.     gettext_noop ("Invalid regular expression"), /* REG_BADPAT */
  953.     gettext_noop ("Invalid collation character"), /* REG_ECOLLATE */
  954.     gettext_noop ("Invalid character class name"), /* REG_ECTYPE */
  955.     gettext_noop ("Trailing backslash"), /* REG_EESCAPE */
  956.     gettext_noop ("Invalid back reference"), /* REG_ESUBREG */
  957.     gettext_noop ("Unmatched [ or [^"),    /* REG_EBRACK */
  958.     gettext_noop ("Unmatched ( or \\("), /* REG_EPAREN */
  959.     gettext_noop ("Unmatched \\{"), /* REG_EBRACE */
  960.     gettext_noop ("Invalid content of \\{\\}"), /* REG_BADBR */
  961.     gettext_noop ("Invalid range end"),    /* REG_ERANGE */
  962.     gettext_noop ("Memory exhausted"), /* REG_ESPACE */
  963.     gettext_noop ("Invalid preceding regular expression"), /* REG_BADRPT */
  964.     gettext_noop ("Premature end of regular expression"), /* REG_EEND */
  965.     gettext_noop ("Regular expression too big"), /* REG_ESIZE */
  966.     gettext_noop ("Unmatched ) or \\)"), /* REG_ERPAREN */
  967.   };
  968.  
  969. /* Avoiding alloca during matching, to placate r_alloc.  */
  970.  
  971. /* Define MATCH_MAY_ALLOCATE unless we need to make sure that the
  972.    searching and matching functions should not call alloca.  On some
  973.    systems, alloca is implemented in terms of malloc, and if we're
  974.    using the relocating allocator routines, then malloc could cause a
  975.    relocation, which might (if the strings being searched are in the
  976.    ralloc heap) shift the data out from underneath the regexp
  977.    routines.
  978.  
  979.    Here's another reason to avoid allocation: Emacs
  980.    processes input from X in a signal handler; processing X input may
  981.    call malloc; if input arrives while a matching routine is calling
  982.    malloc, then we're scrod.  But Emacs can't just block input while
  983.    calling matching routines; then we don't notice interrupts when
  984.    they come in.  So, Emacs blocks input around all regexp calls
  985.    except the matching calls, which it leaves unprotected, in the
  986.    faith that they will not malloc.  */
  987.  
  988. /* Normally, this is fine.  */
  989. #define MATCH_MAY_ALLOCATE
  990.  
  991. /* When using GNU C, we are not REALLY using the C alloca, no matter
  992.    what config.h may say.  So don't take precautions for it.  */
  993. #ifdef __GNUC__
  994. #undef C_ALLOCA
  995. #endif
  996.  
  997. /* The match routines may not allocate if (1) they would do it with malloc
  998.    and (2) it's not safe for them to use malloc.
  999.    Note that if REL_ALLOC is defined, matching would not use malloc for the
  1000.    failure stack, but we would still use it for the register vectors;
  1001.    so REL_ALLOC should not affect this.  */
  1002. #if (defined (C_ALLOCA) || defined (REGEX_MALLOC)) && defined (emacs)
  1003. #undef MATCH_MAY_ALLOCATE
  1004. #endif
  1005.  
  1006.  
  1007. /* Failure stack declarations and macros; both re_compile_fastmap and
  1008.    re_match_2 use a failure stack.  These have to be macros because of
  1009.    REGEX_ALLOCATE_STACK.  */
  1010.  
  1011.  
  1012. /* Number of failure points for which to initially allocate space
  1013.    when matching.  If this number is exceeded, we allocate more
  1014.    space, so it is not a hard limit.  */
  1015. #ifndef INIT_FAILURE_ALLOC
  1016. #define INIT_FAILURE_ALLOC 5
  1017. #endif
  1018.  
  1019. /* Roughly the maximum number of failure points on the stack.  Would be
  1020.    exactly that if always used MAX_FAILURE_SPACE each time we failed.
  1021.    This is a variable only so users of regex can assign to it; we never
  1022.    change it ourselves.  */
  1023.  
  1024. #ifdef INT_IS_16BIT
  1025.  
  1026. #if defined (MATCH_MAY_ALLOCATE)
  1027. long re_max_failures = 20000;
  1028. #else
  1029. long re_max_failures = 2000;
  1030. #endif
  1031.  
  1032. union fail_stack_elt
  1033. {
  1034.   unsigned char *pointer;
  1035.   long integer;
  1036. };
  1037.  
  1038. typedef union fail_stack_elt fail_stack_elt_t;
  1039.  
  1040. typedef struct
  1041. {
  1042.   fail_stack_elt_t *stack;
  1043.   unsigned long size;
  1044.   unsigned long avail;            /* Offset of next open position.  */
  1045. } fail_stack_type;
  1046.  
  1047. #else /* not INT_IS_16BIT */
  1048.  
  1049. #if defined (MATCH_MAY_ALLOCATE)
  1050. int re_max_failures = 20000;
  1051. #else
  1052. int re_max_failures = 2000;
  1053. #endif
  1054.  
  1055. union fail_stack_elt
  1056. {
  1057.   unsigned char *pointer;
  1058.   int integer;
  1059. };
  1060.  
  1061. typedef union fail_stack_elt fail_stack_elt_t;
  1062.  
  1063. typedef struct
  1064. {
  1065.   fail_stack_elt_t *stack;
  1066.   unsigned size;
  1067.   unsigned avail;            /* Offset of next open position.  */
  1068. } fail_stack_type;
  1069.  
  1070. #endif /* INT_IS_16BIT */
  1071.  
  1072. #define FAIL_STACK_EMPTY()     (fail_stack.avail == 0)
  1073. #define FAIL_STACK_PTR_EMPTY() (fail_stack_ptr->avail == 0)
  1074. #define FAIL_STACK_FULL()      (fail_stack.avail == fail_stack.size)
  1075.  
  1076.  
  1077. /* Define macros to initialize and free the failure stack.
  1078.    Do `return -2' if the alloc fails.  */
  1079.  
  1080. #ifdef MATCH_MAY_ALLOCATE
  1081. #define INIT_FAIL_STACK()                        \
  1082.   do {                                    \
  1083.     fail_stack.stack = (fail_stack_elt_t *)                \
  1084.       REGEX_ALLOCATE_STACK (INIT_FAILURE_ALLOC * sizeof (fail_stack_elt_t));    \
  1085.                                     \
  1086.     if (fail_stack.stack == NULL)                    \
  1087.       return -2;                            \
  1088.                                     \
  1089.     fail_stack.size = INIT_FAILURE_ALLOC;                \
  1090.     fail_stack.avail = 0;                        \
  1091.   } while (0)
  1092.  
  1093. #define RESET_FAIL_STACK()  REGEX_FREE_STACK (fail_stack.stack)
  1094. #else
  1095. #define INIT_FAIL_STACK()                        \
  1096.   do {                                    \
  1097.     fail_stack.avail = 0;                        \
  1098.   } while (0)
  1099.  
  1100. #define RESET_FAIL_STACK()
  1101. #endif
  1102.  
  1103.  
  1104. /* Double the size of FAIL_STACK, up to approximately `re_max_failures' items.
  1105.  
  1106.    Return 1 if succeeds, and 0 if either ran out of memory
  1107.    allocating space for it or it was already too large.
  1108.  
  1109.    REGEX_REALLOCATE_STACK requires `destination' be declared.   */
  1110.  
  1111. #define DOUBLE_FAIL_STACK(fail_stack)                    \
  1112.   ((fail_stack).size > re_max_failures * MAX_FAILURE_ITEMS        \
  1113.    ? 0                                    \
  1114.    : ((fail_stack).stack = (fail_stack_elt_t *)                \
  1115.         REGEX_REALLOCATE_STACK ((fail_stack).stack,             \
  1116.           (fail_stack).size * sizeof (fail_stack_elt_t),        \
  1117.           ((fail_stack).size << 1) * sizeof (fail_stack_elt_t)),    \
  1118.                                     \
  1119.       (fail_stack).stack == NULL                    \
  1120.       ? 0                                \
  1121.       : ((fail_stack).size <<= 1,                     \
  1122.          1)))
  1123.  
  1124.  
  1125. /* Push pointer POINTER on FAIL_STACK.
  1126.    Return 1 if was able to do so and 0 if ran out of memory allocating
  1127.    space to do so.  */
  1128. #define PUSH_PATTERN_OP(POINTER, FAIL_STACK)                \
  1129.   ((FAIL_STACK_FULL ()                            \
  1130.     && !DOUBLE_FAIL_STACK (FAIL_STACK))                    \
  1131.    ? 0                                    \
  1132.    : ((FAIL_STACK).stack[(FAIL_STACK).avail++].pointer = POINTER,    \
  1133.       1))
  1134.  
  1135. /* Push a pointer value onto the failure stack.
  1136.    Assumes the variable `fail_stack'.  Probably should only
  1137.    be called from within `PUSH_FAILURE_POINT'.  */
  1138. #define PUSH_FAILURE_POINTER(item)                    \
  1139.   fail_stack.stack[fail_stack.avail++].pointer = (unsigned char *) (item)
  1140.  
  1141. /* This pushes an integer-valued item onto the failure stack.
  1142.    Assumes the variable `fail_stack'.  Probably should only
  1143.    be called from within `PUSH_FAILURE_POINT'.  */
  1144. #define PUSH_FAILURE_INT(item)                    \
  1145.   fail_stack.stack[fail_stack.avail++].integer = (item)
  1146.  
  1147. /* Push a fail_stack_elt_t value onto the failure stack.
  1148.    Assumes the variable `fail_stack'.  Probably should only
  1149.    be called from within `PUSH_FAILURE_POINT'.  */
  1150. #define PUSH_FAILURE_ELT(item)                    \
  1151.   fail_stack.stack[fail_stack.avail++] =  (item)
  1152.  
  1153. /* These three POP... operations complement the three PUSH... operations.
  1154.    All assume that `fail_stack' is nonempty.  */
  1155. #define POP_FAILURE_POINTER() fail_stack.stack[--fail_stack.avail].pointer
  1156. #define POP_FAILURE_INT() fail_stack.stack[--fail_stack.avail].integer
  1157. #define POP_FAILURE_ELT() fail_stack.stack[--fail_stack.avail]
  1158.  
  1159. /* Used to omit pushing failure point id's when we're not debugging.  */
  1160. #ifdef DEBUG
  1161. #define DEBUG_PUSH PUSH_FAILURE_INT
  1162. #define DEBUG_POP(item_addr) (item_addr)->integer = POP_FAILURE_INT ()
  1163. #else
  1164. #define DEBUG_PUSH(item)
  1165. #define DEBUG_POP(item_addr)
  1166. #endif
  1167.  
  1168.  
  1169. /* Push the information about the state we will need
  1170.    if we ever fail back to it.
  1171.  
  1172.    Requires variables fail_stack, regstart, regend, reg_info, and
  1173.    num_regs be declared.  DOUBLE_FAIL_STACK requires `destination' be
  1174.    declared.
  1175.  
  1176.    Does `return FAILURE_CODE' if runs out of memory.  */
  1177.  
  1178. #define PUSH_FAILURE_POINT(pattern_place, string_place, failure_code)    \
  1179.   do {                                    \
  1180.     char *destination;                            \
  1181.     /* Must be int, so when we don't save any registers, the arithmetic    \
  1182.        of 0 + -1 isn't done as unsigned.  */                \
  1183.     /* Can't be int, since there is not a shred of a guarantee that int \
  1184.        is wide enough to hold a value of something to which pointer can \
  1185.        be assigned */                            \
  1186.     s_reg_t this_reg;                            \
  1187.                                         \
  1188.     DEBUG_STATEMENT (failure_id++);                    \
  1189.     DEBUG_STATEMENT (nfailure_points_pushed++);                \
  1190.     DEBUG_PRINT2 ("\nPUSH_FAILURE_POINT #%u:\n", failure_id);        \
  1191.     DEBUG_PRINT2 ("  Before push, next avail: %d\n", (fail_stack).avail);\
  1192.     DEBUG_PRINT2 ("                     size: %d\n", (fail_stack).size);\
  1193.                                     \
  1194.     DEBUG_PRINT2 ("  slots needed: %d\n", NUM_FAILURE_ITEMS);        \
  1195.     DEBUG_PRINT2 ("     available: %d\n", REMAINING_AVAIL_SLOTS);    \
  1196.                                     \
  1197.     /* Ensure we have enough space allocated for what we will push.  */    \
  1198.     while (REMAINING_AVAIL_SLOTS < NUM_FAILURE_ITEMS)            \
  1199.       {                                    \
  1200.         if (!DOUBLE_FAIL_STACK (fail_stack))                \
  1201.           return failure_code;                        \
  1202.                                     \
  1203.         DEBUG_PRINT2 ("\n  Doubled stack; size now: %d\n",        \
  1204.                (fail_stack).size);                \
  1205.         DEBUG_PRINT2 ("  slots available: %d\n", REMAINING_AVAIL_SLOTS);\
  1206.       }                                    \
  1207.                                     \
  1208.     /* Push the info, starting with the registers.  */            \
  1209.     DEBUG_PRINT1 ("\n");                        \
  1210.                                     \
  1211.     if (1)                                \
  1212.       for (this_reg = lowest_active_reg; this_reg <= highest_active_reg; \
  1213.        this_reg++)                            \
  1214.     {                                \
  1215.       DEBUG_PRINT2 ("  Pushing reg: %d\n", this_reg);        \
  1216.       DEBUG_STATEMENT (num_regs_pushed++);                \
  1217.                                     \
  1218.       DEBUG_PRINT2 ("    start: 0x%x\n", regstart[this_reg]);    \
  1219.       PUSH_FAILURE_POINTER (regstart[this_reg]);            \
  1220.                                     \
  1221.       DEBUG_PRINT2 ("    end: 0x%x\n", regend[this_reg]);        \
  1222.       PUSH_FAILURE_POINTER (regend[this_reg]);            \
  1223.                                     \
  1224.       DEBUG_PRINT2 ("    info: 0x%x\n      ", reg_info[this_reg]);    \
  1225.       DEBUG_PRINT2 (" match_null=%d",                \
  1226.             REG_MATCH_NULL_STRING_P (reg_info[this_reg]));    \
  1227.       DEBUG_PRINT2 (" active=%d", IS_ACTIVE (reg_info[this_reg]));    \
  1228.       DEBUG_PRINT2 (" matched_something=%d",            \
  1229.             MATCHED_SOMETHING (reg_info[this_reg]));    \
  1230.       DEBUG_PRINT2 (" ever_matched=%d",                \
  1231.             EVER_MATCHED_SOMETHING (reg_info[this_reg]));    \
  1232.       DEBUG_PRINT1 ("\n");                        \
  1233.       PUSH_FAILURE_ELT (reg_info[this_reg].word);            \
  1234.     }                                \
  1235.                                     \
  1236.     DEBUG_PRINT2 ("  Pushing  low active reg: %d\n", lowest_active_reg);\
  1237.     PUSH_FAILURE_INT (lowest_active_reg);                \
  1238.                                     \
  1239.     DEBUG_PRINT2 ("  Pushing high active reg: %d\n", highest_active_reg);\
  1240.     PUSH_FAILURE_INT (highest_active_reg);                \
  1241.                                     \
  1242.     DEBUG_PRINT2 ("  Pushing pattern 0x%x: ", pattern_place);        \
  1243.     DEBUG_PRINT_COMPILED_PATTERN (bufp, pattern_place, pend);        \
  1244.     PUSH_FAILURE_POINTER (pattern_place);                \
  1245.                                     \
  1246.     DEBUG_PRINT2 ("  Pushing string 0x%x: `", string_place);        \
  1247.     DEBUG_PRINT_DOUBLE_STRING (string_place, string1, size1, string2,   \
  1248.                  size2);                \
  1249.     DEBUG_PRINT1 ("'\n");                        \
  1250.     PUSH_FAILURE_POINTER (string_place);                \
  1251.                                     \
  1252.     DEBUG_PRINT2 ("  Pushing failure id: %u\n", failure_id);        \
  1253.     DEBUG_PUSH (failure_id);                        \
  1254.   } while (0)
  1255.  
  1256. /* This is the number of items that are pushed and popped on the stack
  1257.    for each register.  */
  1258. #define NUM_REG_ITEMS  3
  1259.  
  1260. /* Individual items aside from the registers.  */
  1261. #ifdef DEBUG
  1262. #define NUM_NONREG_ITEMS 5 /* Includes failure point id.  */
  1263. #else
  1264. #define NUM_NONREG_ITEMS 4
  1265. #endif
  1266.  
  1267. /* We push at most this many items on the stack.  */
  1268. #define MAX_FAILURE_ITEMS ((num_regs - 1) * NUM_REG_ITEMS + NUM_NONREG_ITEMS)
  1269.  
  1270. /* We actually push this many items.  */
  1271. #define NUM_FAILURE_ITEMS                \
  1272.   (((0                            \
  1273.      ? 0 : highest_active_reg - lowest_active_reg + 1)    \
  1274.     * NUM_REG_ITEMS)                    \
  1275.    + NUM_NONREG_ITEMS)
  1276.  
  1277. /* How many items can still be added to the stack without overflowing it.  */
  1278. #define REMAINING_AVAIL_SLOTS ((fail_stack).size - (fail_stack).avail)
  1279.  
  1280.  
  1281. /* Pops what PUSH_FAIL_STACK pushes.
  1282.  
  1283.    We restore into the parameters, all of which should be lvalues:
  1284.      STR -- the saved data position.
  1285.      PAT -- the saved pattern position.
  1286.      LOW_REG, HIGH_REG -- the highest and lowest active registers.
  1287.      REGSTART, REGEND -- arrays of string positions.
  1288.      REG_INFO -- array of information about each subexpression.
  1289.  
  1290.    Also assumes the variables `fail_stack' and (if debugging), `bufp',
  1291.    `pend', `string1', `size1', `string2', and `size2'.  */
  1292.  
  1293. #define POP_FAILURE_POINT(str, pat, low_reg, high_reg, regstart, regend, reg_info)\
  1294. {                                    \
  1295.   DEBUG_STATEMENT (fail_stack_elt_t failure_id;)            \
  1296.   s_reg_t this_reg;                            \
  1297.   const unsigned char *string_temp;                    \
  1298.                                     \
  1299.   assert (!FAIL_STACK_EMPTY ());                    \
  1300.                                     \
  1301.   /* Remove failure points and point to how many regs pushed.  */    \
  1302.   DEBUG_PRINT1 ("POP_FAILURE_POINT:\n");                \
  1303.   DEBUG_PRINT2 ("  Before pop, next avail: %d\n", fail_stack.avail);    \
  1304.   DEBUG_PRINT2 ("                    size: %d\n", fail_stack.size);    \
  1305.                                     \
  1306.   assert (fail_stack.avail >= NUM_NONREG_ITEMS);            \
  1307.                                     \
  1308.   DEBUG_POP (&failure_id);                        \
  1309.   DEBUG_PRINT2 ("  Popping failure id: %u\n", failure_id);        \
  1310.                                     \
  1311.   /* If the saved string location is NULL, it came from an        \
  1312.      on_failure_keep_string_jump opcode, and we want to throw away the    \
  1313.      saved NULL, thus retaining our current position in the string.  */    \
  1314.   string_temp = POP_FAILURE_POINTER ();                    \
  1315.   if (string_temp != NULL)                        \
  1316.     str = (const char *) string_temp;                    \
  1317.                                     \
  1318.   DEBUG_PRINT2 ("  Popping string 0x%x: `", str);            \
  1319.   DEBUG_PRINT_DOUBLE_STRING (str, string1, size1, string2, size2);    \
  1320.   DEBUG_PRINT1 ("'\n");                            \
  1321.                                     \
  1322.   pat = (unsigned char *) POP_FAILURE_POINTER ();            \
  1323.   DEBUG_PRINT2 ("  Popping pattern 0x%x: ", pat);            \
  1324.   DEBUG_PRINT_COMPILED_PATTERN (bufp, pat, pend);            \
  1325.                                     \
  1326.   /* Restore register info.  */                        \
  1327.   high_reg = (active_reg_t) POP_FAILURE_INT ();                \
  1328.   DEBUG_PRINT2 ("  Popping high active reg: %d\n", high_reg);        \
  1329.                                     \
  1330.   low_reg = (active_reg_t) POP_FAILURE_INT ();                \
  1331.   DEBUG_PRINT2 ("  Popping  low active reg: %d\n", low_reg);        \
  1332.                                     \
  1333.   if (1)                                \
  1334.     for (this_reg = high_reg; this_reg >= low_reg; this_reg--)        \
  1335.       {                                    \
  1336.     DEBUG_PRINT2 ("    Popping reg: %d\n", this_reg);        \
  1337.                                     \
  1338.     reg_info[this_reg].word = POP_FAILURE_ELT ();            \
  1339.     DEBUG_PRINT2 ("      info: 0x%x\n", reg_info[this_reg]);    \
  1340.                                     \
  1341.     regend[this_reg] = (const char *) POP_FAILURE_POINTER ();    \
  1342.     DEBUG_PRINT2 ("      end: 0x%x\n", regend[this_reg]);        \
  1343.                                     \
  1344.     regstart[this_reg] = (const char *) POP_FAILURE_POINTER ();    \
  1345.     DEBUG_PRINT2 ("      start: 0x%x\n", regstart[this_reg]);    \
  1346.       }                                    \
  1347.   else                                    \
  1348.     {                                    \
  1349.       for (this_reg = highest_active_reg; this_reg > high_reg; this_reg--) \
  1350.     {                                \
  1351.       reg_info[this_reg].word.integer = 0;                \
  1352.       regend[this_reg] = 0;                        \
  1353.       regstart[this_reg] = 0;                    \
  1354.     }                                \
  1355.       highest_active_reg = high_reg;                    \
  1356.     }                                    \
  1357.                                     \
  1358.   set_regs_matched_done = 0;                        \
  1359.   DEBUG_STATEMENT (nfailure_points_popped++);                \
  1360. } /* POP_FAILURE_POINT */
  1361.  
  1362.  
  1363.  
  1364. /* Structure for per-register (a.k.a. per-group) information.
  1365.    Other register information, such as the
  1366.    starting and ending positions (which are addresses), and the list of
  1367.    inner groups (which is a bits list) are maintained in separate
  1368.    variables.
  1369.  
  1370.    We are making a (strictly speaking) nonportable assumption here: that
  1371.    the compiler will pack our bit fields into something that fits into
  1372.    the type of `word', i.e., is something that fits into one item on the
  1373.    failure stack.  */
  1374.  
  1375.  
  1376. /* Declarations and macros for re_match_2.  */
  1377.  
  1378. typedef union
  1379. {
  1380.   fail_stack_elt_t word;
  1381.   struct
  1382.   {
  1383.       /* This field is one if this group can match the empty string,
  1384.          zero if not.  If not yet determined,  `MATCH_NULL_UNSET_VALUE'.  */
  1385. #define MATCH_NULL_UNSET_VALUE 3
  1386.     unsigned match_null_string_p : 2;
  1387.     unsigned is_active : 1;
  1388.     unsigned matched_something : 1;
  1389.     unsigned ever_matched_something : 1;
  1390.   } bits;
  1391. } register_info_type;
  1392.  
  1393. #define REG_MATCH_NULL_STRING_P(R)  ((R).bits.match_null_string_p)
  1394. #define IS_ACTIVE(R)  ((R).bits.is_active)
  1395. #define MATCHED_SOMETHING(R)  ((R).bits.matched_something)
  1396. #define EVER_MATCHED_SOMETHING(R)  ((R).bits.ever_matched_something)
  1397.  
  1398.  
  1399. /* Call this when have matched a real character; it sets `matched' flags
  1400.    for the subexpressions which we are currently inside.  Also records
  1401.    that those subexprs have matched.  */
  1402. #define SET_REGS_MATCHED()                        \
  1403.   do                                    \
  1404.     {                                    \
  1405.       if (!set_regs_matched_done)                    \
  1406.     {                                \
  1407.       active_reg_t r;                        \
  1408.       set_regs_matched_done = 1;                    \
  1409.       for (r = lowest_active_reg; r <= highest_active_reg; r++)    \
  1410.         {                                \
  1411.           MATCHED_SOMETHING (reg_info[r])                \
  1412.         = EVER_MATCHED_SOMETHING (reg_info[r])            \
  1413.         = 1;                            \
  1414.         }                                \
  1415.     }                                \
  1416.     }                                    \
  1417.   while (0)
  1418.  
  1419. /* Registers are set to a sentinel when they haven't yet matched.  */
  1420. static char reg_unset_dummy;
  1421. #define REG_UNSET_VALUE (®_unset_dummy)
  1422. #define REG_UNSET(e) ((e) == REG_UNSET_VALUE)
  1423.  
  1424. /* Subroutine declarations and macros for regex_compile.  */
  1425.  
  1426. static reg_errcode_t regex_compile _RE_ARGS((const char *pattern, size_t size,
  1427.                          reg_syntax_t syntax,
  1428.                          struct re_pattern_buffer *bufp));
  1429. static void store_op1 _RE_ARGS((re_opcode_t op, unsigned char *loc, int arg));
  1430. static void store_op2 _RE_ARGS((re_opcode_t op, unsigned char *loc,
  1431.                 int arg1, int arg2));
  1432. static void insert_op1 _RE_ARGS((re_opcode_t op, unsigned char *loc,
  1433.                  int arg, unsigned char *end));
  1434. static void insert_op2 _RE_ARGS((re_opcode_t op, unsigned char *loc,
  1435.                  int arg1, int arg2, unsigned char *end));
  1436. static boolean at_begline_loc_p _RE_ARGS((const char *pattern, const char *p,
  1437.                       reg_syntax_t syntax));
  1438. static boolean at_endline_loc_p _RE_ARGS((const char *p, const char *pend,
  1439.                       reg_syntax_t syntax));
  1440. static reg_errcode_t compile_range _RE_ARGS((const char **p_ptr,
  1441.                          const char *pend,
  1442.                          char *translate,
  1443.                          reg_syntax_t syntax,
  1444.                          unsigned char *b));
  1445.  
  1446. /* Fetch the next character in the uncompiled pattern---translating it
  1447.    if necessary.  Also cast from a signed character in the constant
  1448.    string passed to us by the user to an unsigned char that we can use
  1449.    as an array index (in, e.g., `translate').  */
  1450. #ifndef PATFETCH
  1451. #define PATFETCH(c)                            \
  1452.   do {if (p == pend) return REG_EEND;                    \
  1453.     c = (unsigned char) *p++;                        \
  1454.     if (translate) c = (unsigned char) translate[c];            \
  1455.   } while (0)
  1456. #endif
  1457.  
  1458. /* Fetch the next character in the uncompiled pattern, with no
  1459.    translation.  */
  1460. #define PATFETCH_RAW(c)                            \
  1461.   do {if (p == pend) return REG_EEND;                    \
  1462.     c = (unsigned char) *p++;                         \
  1463.   } while (0)
  1464.  
  1465. /* Go backwards one character in the pattern.  */
  1466. #define PATUNFETCH p--
  1467.  
  1468.  
  1469. /* If `translate' is non-null, return translate[D], else just D.  We
  1470.    cast the subscript to translate because some data is declared as
  1471.    `char *', to avoid warnings when a string constant is passed.  But
  1472.    when we use a character as a subscript we must make it unsigned.  */
  1473. #ifndef TRANSLATE
  1474. #define TRANSLATE(d) \
  1475.   (translate ? (char) translate[(unsigned char) (d)] : (d))
  1476. #endif
  1477.  
  1478.  
  1479. /* Macros for outputting the compiled pattern into `buffer'.  */
  1480.  
  1481. /* If the buffer isn't allocated when it comes in, use this.  */
  1482. #define INIT_BUF_SIZE  32
  1483.  
  1484. /* Make sure we have at least N more bytes of space in buffer.  */
  1485. #define GET_BUFFER_SPACE(n)                        \
  1486.     while (b - bufp->buffer + (n) > bufp->allocated)            \
  1487.       EXTEND_BUFFER ()
  1488.  
  1489. /* Make sure we have one more byte of buffer space and then add C to it.  */
  1490. #define BUF_PUSH(c)                            \
  1491.   do {                                    \
  1492.     GET_BUFFER_SPACE (1);                        \
  1493.     *b++ = (unsigned char) (c);                        \
  1494.   } while (0)
  1495.  
  1496.  
  1497. /* Ensure we have two more bytes of buffer space and then append C1 and C2.  */
  1498. #define BUF_PUSH_2(c1, c2)                        \
  1499.   do {                                    \
  1500.     GET_BUFFER_SPACE (2);                        \
  1501.     *b++ = (unsigned char) (c1);                    \
  1502.     *b++ = (unsigned char) (c2);                    \
  1503.   } while (0)
  1504.  
  1505.  
  1506. /* As with BUF_PUSH_2, except for three bytes.  */
  1507. #define BUF_PUSH_3(c1, c2, c3)                        \
  1508.   do {                                    \
  1509.     GET_BUFFER_SPACE (3);                        \
  1510.     *b++ = (unsigned char) (c1);                    \
  1511.     *b++ = (unsigned char) (c2);                    \
  1512.     *b++ = (unsigned char) (c3);                    \
  1513.   } while (0)
  1514.  
  1515.  
  1516. /* Store a jump with opcode OP at LOC to location TO.  We store a
  1517.    relative address offset by the three bytes the jump itself occupies.  */
  1518. #define STORE_JUMP(op, loc, to) \
  1519.   store_op1 (op, loc, (int)((to) - (loc) - 3))
  1520.  
  1521. /* Likewise, for a two-argument jump.  */
  1522. #define STORE_JUMP2(op, loc, to, arg) \
  1523.   store_op2 (op, loc, (int)((to) - (loc) - 3), arg)
  1524.  
  1525. /* Like `STORE_JUMP', but for inserting.  Assume `b' is the buffer end.  */
  1526. #define INSERT_JUMP(op, loc, to) \
  1527.   insert_op1 (op, loc, (int)((to) - (loc) - 3), b)
  1528.  
  1529. /* Like `STORE_JUMP2', but for inserting.  Assume `b' is the buffer end.  */
  1530. #define INSERT_JUMP2(op, loc, to, arg) \
  1531.   insert_op2 (op, loc, (int)((to) - (loc) - 3), arg, b)
  1532.  
  1533.  
  1534. /* This is not an arbitrary limit: the arguments which represent offsets
  1535.    into the pattern are two bytes long.  So if 2^16 bytes turns out to
  1536.    be too small, many things would have to change.  */
  1537. /* Any other compiler which, like MSC, has allocation limit below 2^16
  1538.    bytes will have to use approach similar to what was done below for
  1539.    MSC and drop MAX_BUF_SIZE a bit.  Otherwise you may end up
  1540.    reallocating to 0 bytes.  Such thing is not going to work too well.
  1541.    You have been warned!!  */
  1542. #ifdef _MSC_VER
  1543. /* Microsoft C 16-bit versions limit malloc to approx 65512 bytes.
  1544.    The REALLOC define eliminates a flurry of conversion warnings,
  1545.    but is not required. */
  1546. #define MAX_BUF_SIZE  65500L
  1547. #define REALLOC(p,s) realloc((p), (size_t) (s))
  1548. #else
  1549. #define MAX_BUF_SIZE (1L << 16)
  1550. #define REALLOC realloc
  1551. #endif
  1552.  
  1553. /* Extend the buffer by twice its current size via realloc and
  1554.    reset the pointers that pointed into the old block to point to the
  1555.    correct places in the new one.  If extending the buffer results in it
  1556.    being larger than MAX_BUF_SIZE, then flag memory exhausted.  */
  1557. #define EXTEND_BUFFER()                            \
  1558.   do {                                     \
  1559.     unsigned char *old_buffer = bufp->buffer;                \
  1560.     if (bufp->allocated == MAX_BUF_SIZE)                 \
  1561.       return REG_ESIZE;                            \
  1562.     bufp->allocated <<= 1;                        \
  1563.     if (bufp->allocated > MAX_BUF_SIZE)                    \
  1564.       bufp->allocated = MAX_BUF_SIZE;                     \
  1565.     bufp->buffer = (unsigned char *) REALLOC(bufp->buffer, bufp->allocated);\
  1566.     if (bufp->buffer == NULL)                        \
  1567.       return REG_ESPACE;                        \
  1568.     /* If the buffer moved, move all the pointers into it.  */        \
  1569.     if (old_buffer != bufp->buffer)                    \
  1570.       {                                    \
  1571.         b = (b - old_buffer) + bufp->buffer;                \
  1572.         begalt = (begalt - old_buffer) + bufp->buffer;            \
  1573.         if (fixup_alt_jump)                        \
  1574.           fixup_alt_jump = (fixup_alt_jump - old_buffer) + bufp->buffer;\
  1575.         if (laststart)                            \
  1576.           laststart = (laststart - old_buffer) + bufp->buffer;        \
  1577.         if (pending_exact)                        \
  1578.           pending_exact = (pending_exact - old_buffer) + bufp->buffer;    \
  1579.       }                                    \
  1580.   } while (0)
  1581.  
  1582.  
  1583. /* Since we have one byte reserved for the register number argument to
  1584.    {start,stop}_memory, the maximum number of groups we can report
  1585.    things about is what fits in that byte.  */
  1586. #define MAX_REGNUM 255
  1587.  
  1588. /* But patterns can have more than `MAX_REGNUM' registers.  We just
  1589.    ignore the excess.  */
  1590. typedef unsigned regnum_t;
  1591.  
  1592.  
  1593. /* Macros for the compile stack.  */
  1594.  
  1595. /* Since offsets can go either forwards or backwards, this type needs to
  1596.    be able to hold values from -(MAX_BUF_SIZE - 1) to MAX_BUF_SIZE - 1.  */
  1597. /* int may be not enough when sizeof(int) == 2                           */
  1598. typedef long pattern_offset_t;
  1599.  
  1600. typedef struct
  1601. {
  1602.   pattern_offset_t begalt_offset;
  1603.   pattern_offset_t fixup_alt_jump;
  1604.   pattern_offset_t inner_group_offset;
  1605.   pattern_offset_t laststart_offset;
  1606.   regnum_t regnum;
  1607. } compile_stack_elt_t;
  1608.  
  1609.  
  1610. typedef struct
  1611. {
  1612.   compile_stack_elt_t *stack;
  1613.   unsigned size;
  1614.   unsigned avail;            /* Offset of next open position.  */
  1615. } compile_stack_type;
  1616.  
  1617.  
  1618. #define INIT_COMPILE_STACK_SIZE 32
  1619.  
  1620. #define COMPILE_STACK_EMPTY  (compile_stack.avail == 0)
  1621. #define COMPILE_STACK_FULL  (compile_stack.avail == compile_stack.size)
  1622.  
  1623. /* The next available element.  */
  1624. #define COMPILE_STACK_TOP (compile_stack.stack[compile_stack.avail])
  1625.  
  1626.  
  1627. /* Set the bit for character C in a list.  */
  1628. #define SET_LIST_BIT(c)                               \
  1629.   (b[((unsigned char) (c)) / BYTEWIDTH]               \
  1630.    |= 1 << (((unsigned char) c) % BYTEWIDTH))
  1631.  
  1632.  
  1633. /* Get the next unsigned number in the uncompiled pattern.  */
  1634. #define GET_UNSIGNED_NUMBER(num)                     \
  1635.   { if (p != pend)                            \
  1636.      {                                    \
  1637.        PATFETCH (c);                             \
  1638.        while (ISDIGIT (c))                         \
  1639.          {                                 \
  1640.            if (num < 0)                            \
  1641.               num = 0;                            \
  1642.            num = num * 10 + c - '0';                     \
  1643.            if (p == pend)                         \
  1644.               break;                             \
  1645.            PATFETCH (c);                        \
  1646.          }                                 \
  1647.        }                                 \
  1648.     }
  1649.  
  1650. #define CHAR_CLASS_MAX_LENGTH  6 /* Namely, `xdigit'.  */
  1651.  
  1652. #define IS_CHAR_CLASS(string)                        \
  1653.    (STREQ (string, "alpha") || STREQ (string, "upper")            \
  1654.     || STREQ (string, "lower") || STREQ (string, "digit")        \
  1655.     || STREQ (string, "alnum") || STREQ (string, "xdigit")        \
  1656.     || STREQ (string, "space") || STREQ (string, "print")        \
  1657.     || STREQ (string, "punct") || STREQ (string, "graph")        \
  1658.     || STREQ (string, "cntrl") || STREQ (string, "blank"))
  1659.  
  1660. #ifndef MATCH_MAY_ALLOCATE
  1661.  
  1662. /* If we cannot allocate large objects within re_match_2_internal,
  1663.    we make the fail stack and register vectors global.
  1664.    The fail stack, we grow to the maximum size when a regexp
  1665.    is compiled.
  1666.    The register vectors, we adjust in size each time we
  1667.    compile a regexp, according to the number of registers it needs.  */
  1668.  
  1669. static fail_stack_type fail_stack;
  1670.  
  1671. /* Size with which the following vectors are currently allocated.
  1672.    That is so we can make them bigger as needed,
  1673.    but never make them smaller.  */
  1674. static int regs_allocated_size;
  1675.  
  1676. static const char **     regstart, **     regend;
  1677. static const char ** old_regstart, ** old_regend;
  1678. static const char **best_regstart, **best_regend;
  1679. static register_info_type *reg_info;
  1680. static const char **reg_dummy;
  1681. static register_info_type *reg_info_dummy;
  1682.  
  1683. /* Make the register vectors big enough for NUM_REGS registers,
  1684.    but don't make them smaller.  */
  1685.  
  1686. static
  1687. regex_grow_registers (num_regs)
  1688.      int num_regs;
  1689. {
  1690.   if (num_regs > regs_allocated_size)
  1691.     {
  1692.       RETALLOC_IF (regstart,     num_regs, const char *);
  1693.       RETALLOC_IF (regend,     num_regs, const char *);
  1694.       RETALLOC_IF (old_regstart, num_regs, const char *);
  1695.       RETALLOC_IF (old_regend,     num_regs, const char *);
  1696.       RETALLOC_IF (best_regstart, num_regs, const char *);
  1697.       RETALLOC_IF (best_regend,     num_regs, const char *);
  1698.       RETALLOC_IF (reg_info,     num_regs, register_info_type);
  1699.       RETALLOC_IF (reg_dummy,     num_regs, const char *);
  1700.       RETALLOC_IF (reg_info_dummy, num_regs, register_info_type);
  1701.  
  1702.       regs_allocated_size = num_regs;
  1703.     }
  1704. }
  1705.  
  1706. #endif /* not MATCH_MAY_ALLOCATE */
  1707.  
  1708. static boolean group_in_compile_stack _RE_ARGS((compile_stack_type
  1709.                         compile_stack,
  1710.                         regnum_t regnum));
  1711.  
  1712. /* `regex_compile' compiles PATTERN (of length SIZE) according to SYNTAX.
  1713.    Returns one of error codes defined in `regex.h', or zero for success.
  1714.  
  1715.    Assumes the `allocated' (and perhaps `buffer') and `translate'
  1716.    fields are set in BUFP on entry.
  1717.  
  1718.    If it succeeds, results are put in BUFP (if it returns an error, the
  1719.    contents of BUFP are undefined):
  1720.      `buffer' is the compiled pattern;
  1721.      `syntax' is set to SYNTAX;
  1722.      `used' is set to the length of the compiled pattern;
  1723.      `fastmap_accurate' is zero;
  1724.      `re_nsub' is the number of subexpressions in PATTERN;
  1725.      `not_bol' and `not_eol' are zero;
  1726.  
  1727.    The `fastmap' and `newline_anchor' fields are neither
  1728.    examined nor set.  */
  1729.  
  1730. /* Return, freeing storage we allocated.  */
  1731. #define FREE_STACK_RETURN(value)        \
  1732.   return (free (compile_stack.stack), value)
  1733.  
  1734. static reg_errcode_t
  1735. regex_compile (pattern, size, syntax, bufp)
  1736.      const char *pattern;
  1737.      size_t size;
  1738.      reg_syntax_t syntax;
  1739.      struct re_pattern_buffer *bufp;
  1740. {
  1741.   /* We fetch characters from PATTERN here.  Even though PATTERN is
  1742.      `char *' (i.e., signed), we declare these variables as unsigned, so
  1743.      they can be reliably used as array indices.  */
  1744.   register unsigned char c, c1;
  1745.  
  1746.   /* A random temporary spot in PATTERN.  */
  1747.   const char *p1;
  1748.  
  1749.   /* Points to the end of the buffer, where we should append.  */
  1750.   register unsigned char *b;
  1751.  
  1752.   /* Keeps track of unclosed groups.  */
  1753.   compile_stack_type compile_stack;
  1754.  
  1755.   /* Points to the current (ending) position in the pattern.  */
  1756.   const char *p = pattern;
  1757.   const char *pend = pattern + size;
  1758.  
  1759.   /* How to translate the characters in the pattern.  */
  1760.   RE_TRANSLATE_TYPE translate = bufp->translate;
  1761.  
  1762.   /* Address of the count-byte of the most recently inserted `exactn'
  1763.      command.  This makes it possible to tell if a new exact-match
  1764.      character can be added to that command or if the character requires
  1765.      a new `exactn' command.  */
  1766.   unsigned char *pending_exact = 0;
  1767.  
  1768.   /* Address of start of the most recently finished expression.
  1769.      This tells, e.g., postfix * where to find the start of its
  1770.      operand.  Reset at the beginning of groups and alternatives.  */
  1771.   unsigned char *laststart = 0;
  1772.  
  1773.   /* Address of beginning of regexp, or inside of last group.  */
  1774.   unsigned char *begalt;
  1775.  
  1776.   /* Place in the uncompiled pattern (i.e., the {) to
  1777.      which to go back if the interval is invalid.  */
  1778.   const char *beg_interval;
  1779.  
  1780.   /* Address of the place where a forward jump should go to the end of
  1781.      the containing expression.  Each alternative of an `or' -- except the
  1782.      last -- ends with a forward jump of this sort.  */
  1783.   unsigned char *fixup_alt_jump = 0;
  1784.  
  1785.   /* Counts open-groups as they are encountered.  Remembered for the
  1786.      matching close-group on the compile stack, so the same register
  1787.      number is put in the stop_memory as the start_memory.  */
  1788.   regnum_t regnum = 0;
  1789.  
  1790. #ifdef DEBUG
  1791.   DEBUG_PRINT1 ("\nCompiling pattern: ");
  1792.   if (debug)
  1793.     {
  1794.       unsigned debug_count;
  1795.  
  1796.       for (debug_count = 0; debug_count < size; debug_count++)
  1797.         putchar (pattern[debug_count]);
  1798.       putchar ('\n');
  1799.     }
  1800. #endif /* DEBUG */
  1801.  
  1802.   /* Initialize the compile stack.  */
  1803.   compile_stack.stack = TALLOC (INIT_COMPILE_STACK_SIZE, compile_stack_elt_t);
  1804.   if (compile_stack.stack == NULL)
  1805.     return REG_ESPACE;
  1806.  
  1807.   compile_stack.size = INIT_COMPILE_STACK_SIZE;
  1808.   compile_stack.avail = 0;
  1809.  
  1810.   /* Initialize the pattern buffer.  */
  1811.   bufp->syntax = syntax;
  1812.   bufp->fastmap_accurate = 0;
  1813.   bufp->not_bol = bufp->not_eol = 0;
  1814.  
  1815.   /* Set `used' to zero, so that if we return an error, the pattern
  1816.      printer (for debugging) will think there's no pattern.  We reset it
  1817.      at the end.  */
  1818.   bufp->used = 0;
  1819.  
  1820.   /* Always count groups, whether or not bufp->no_sub is set.  */
  1821.   bufp->re_nsub = 0;
  1822.  
  1823. #if !defined (emacs) && !defined (SYNTAX_TABLE)
  1824.   /* Initialize the syntax table.  */
  1825.    init_syntax_once ();
  1826. #endif
  1827.  
  1828.   if (bufp->allocated == 0)
  1829.     {
  1830.       if (bufp->buffer)
  1831.     { /* If zero allocated, but buffer is non-null, try to realloc
  1832.              enough space.  This loses if buffer's address is bogus, but
  1833.              that is the user's responsibility.  */
  1834.           RETALLOC (bufp->buffer, INIT_BUF_SIZE, unsigned char);
  1835.         }
  1836.       else
  1837.         { /* Caller did not allocate a buffer.  Do it for them.  */
  1838.           bufp->buffer = TALLOC (INIT_BUF_SIZE, unsigned char);
  1839.         }
  1840.       if (!bufp->buffer) FREE_STACK_RETURN (REG_ESPACE);
  1841.  
  1842.       bufp->allocated = INIT_BUF_SIZE;
  1843.     }
  1844.  
  1845.   begalt = b = bufp->buffer;
  1846.  
  1847.   /* Loop through the uncompiled pattern until we're at the end.  */
  1848.   while (p != pend)
  1849.     {
  1850.       PATFETCH (c);
  1851.  
  1852.       switch (c)
  1853.         {
  1854.         case '^':
  1855.           {
  1856.             if (   /* If at start of pattern, it's an operator.  */
  1857.                    p == pattern + 1
  1858.                    /* If context independent, it's an operator.  */
  1859.                 || syntax & RE_CONTEXT_INDEP_ANCHORS
  1860.                    /* Otherwise, depends on what's come before.  */
  1861.                 || at_begline_loc_p (pattern, p, syntax))
  1862.               BUF_PUSH (begline);
  1863.             else
  1864.               goto normal_char;
  1865.           }
  1866.           break;
  1867.  
  1868.  
  1869.         case '$':
  1870.           {
  1871.             if (   /* If at end of pattern, it's an operator.  */
  1872.                    p == pend
  1873.                    /* If context independent, it's an operator.  */
  1874.                 || syntax & RE_CONTEXT_INDEP_ANCHORS
  1875.                    /* Otherwise, depends on what's next.  */
  1876.                 || at_endline_loc_p (p, pend, syntax))
  1877.                BUF_PUSH (endline);
  1878.              else
  1879.                goto normal_char;
  1880.            }
  1881.            break;
  1882.  
  1883.  
  1884.     case '+':
  1885.         case '?':
  1886.           if ((syntax & RE_BK_PLUS_QM)
  1887.               || (syntax & RE_LIMITED_OPS))
  1888.             goto normal_char;
  1889.         handle_plus:
  1890.         case '*':
  1891.           /* If there is no previous pattern... */
  1892.           if (!laststart)
  1893.             {
  1894.               if (syntax & RE_CONTEXT_INVALID_OPS)
  1895.                 FREE_STACK_RETURN (REG_BADRPT);
  1896.               else if (!(syntax & RE_CONTEXT_INDEP_OPS))
  1897.                 goto normal_char;
  1898.             }
  1899.  
  1900.           {
  1901.             /* Are we optimizing this jump?  */
  1902.             boolean keep_string_p = false;
  1903.  
  1904.             /* 1 means zero (many) matches is allowed.  */
  1905.             char zero_times_ok = 0, many_times_ok = 0;
  1906.  
  1907.             /* If there is a sequence of repetition chars, collapse it
  1908.                down to just one (the right one).  We can't combine
  1909.                interval operators with these because of, e.g., `a{2}*',
  1910.                which should only match an even number of `a's.  */
  1911.  
  1912.             for (;;)
  1913.               {
  1914.                 zero_times_ok |= c != '+';
  1915.                 many_times_ok |= c != '?';
  1916.  
  1917.                 if (p == pend)
  1918.                   break;
  1919.  
  1920.                 PATFETCH (c);
  1921.  
  1922.                 if (c == '*'
  1923.                     || (!(syntax & RE_BK_PLUS_QM) && (c == '+' || c == '?')))
  1924.                   ;
  1925.  
  1926.                 else if (syntax & RE_BK_PLUS_QM  &&  c == '\\')
  1927.                   {
  1928.                     if (p == pend) FREE_STACK_RETURN (REG_EESCAPE);
  1929.  
  1930.                     PATFETCH (c1);
  1931.                     if (!(c1 == '+' || c1 == '?'))
  1932.                       {
  1933.                         PATUNFETCH;
  1934.                         PATUNFETCH;
  1935.                         break;
  1936.                       }
  1937.  
  1938.                     c = c1;
  1939.                   }
  1940.                 else
  1941.                   {
  1942.                     PATUNFETCH;
  1943.                     break;
  1944.                   }
  1945.  
  1946.                 /* If we get here, we found another repeat character.  */
  1947.                }
  1948.  
  1949.             /* Star, etc. applied to an empty pattern is equivalent
  1950.                to an empty pattern.  */
  1951.             if (!laststart)
  1952.               break;
  1953.  
  1954.             /* Now we know whether or not zero matches is allowed
  1955.                and also whether or not two or more matches is allowed.  */
  1956.             if (many_times_ok)
  1957.               { /* More than one repetition is allowed, so put in at the
  1958.                    end a backward relative jump from `b' to before the next
  1959.                    jump we're going to put in below (which jumps from
  1960.                    laststart to after this jump).
  1961.  
  1962.                    But if we are at the `*' in the exact sequence `.*\n',
  1963.                    insert an unconditional jump backwards to the .,
  1964.                    instead of the beginning of the loop.  This way we only
  1965.                    push a failure point once, instead of every time
  1966.                    through the loop.  */
  1967.                 assert (p - 1 > pattern);
  1968.  
  1969.                 /* Allocate the space for the jump.  */
  1970.                 GET_BUFFER_SPACE (3);
  1971.  
  1972.                 /* We know we are not at the first character of the pattern,
  1973.                    because laststart was nonzero.  And we've already
  1974.                    incremented `p', by the way, to be the character after
  1975.                    the `*'.  Do we have to do something analogous here
  1976.                    for null bytes, because of RE_DOT_NOT_NULL?  */
  1977.                 if (TRANSLATE (*(p - 2)) == TRANSLATE ('.')
  1978.             && zero_times_ok
  1979.                     && p < pend && TRANSLATE (*p) == TRANSLATE ('\n')
  1980.                     && !(syntax & RE_DOT_NEWLINE))
  1981.                   { /* We have .*\n.  */
  1982.                     STORE_JUMP (jump, b, laststart);
  1983.                     keep_string_p = true;
  1984.                   }
  1985.                 else
  1986.                   /* Anything else.  */
  1987.                   STORE_JUMP (maybe_pop_jump, b, laststart - 3);
  1988.  
  1989.                 /* We've added more stuff to the buffer.  */
  1990.                 b += 3;
  1991.               }
  1992.  
  1993.             /* On failure, jump from laststart to b + 3, which will be the
  1994.                end of the buffer after this jump is inserted.  */
  1995.             GET_BUFFER_SPACE (3);
  1996.             INSERT_JUMP (keep_string_p ? on_failure_keep_string_jump
  1997.                                        : on_failure_jump,
  1998.                          laststart, b + 3);
  1999.             pending_exact = 0;
  2000.             b += 3;
  2001.  
  2002.             if (!zero_times_ok)
  2003.               {
  2004.                 /* At least one repetition is required, so insert a
  2005.                    `dummy_failure_jump' before the initial
  2006.                    `on_failure_jump' instruction of the loop. This
  2007.                    effects a skip over that instruction the first time
  2008.                    we hit that loop.  */
  2009.                 GET_BUFFER_SPACE (3);
  2010.                 INSERT_JUMP (dummy_failure_jump, laststart, laststart + 6);
  2011.                 b += 3;
  2012.               }
  2013.             }
  2014.       break;
  2015.  
  2016.  
  2017.     case '.':
  2018.           laststart = b;
  2019.           BUF_PUSH (anychar);
  2020.           break;
  2021.  
  2022.  
  2023.         case '[':
  2024.           {
  2025.             boolean had_char_class = false;
  2026.  
  2027.             if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
  2028.  
  2029.             /* Ensure that we have enough space to push a charset: the
  2030.                opcode, the length count, and the bitset; 34 bytes in all.  */
  2031.         GET_BUFFER_SPACE (34);
  2032.  
  2033.             laststart = b;
  2034.  
  2035.             /* We test `*p == '^' twice, instead of using an if
  2036.                statement, so we only need one BUF_PUSH.  */
  2037.             BUF_PUSH (*p == '^' ? charset_not : charset);
  2038.             if (*p == '^')
  2039.               p++;
  2040.  
  2041.             /* Remember the first position in the bracket expression.  */
  2042.             p1 = p;
  2043.  
  2044.             /* Push the number of bytes in the bitmap.  */
  2045.             BUF_PUSH ((1 << BYTEWIDTH) / BYTEWIDTH);
  2046.  
  2047.             /* Clear the whole map.  */
  2048.             bzero (b, (1 << BYTEWIDTH) / BYTEWIDTH);
  2049.  
  2050.             /* charset_not matches newline according to a syntax bit.  */
  2051.             if ((re_opcode_t) b[-2] == charset_not
  2052.                 && (syntax & RE_HAT_LISTS_NOT_NEWLINE))
  2053.               SET_LIST_BIT ('\n');
  2054.  
  2055.             /* Read in characters and ranges, setting map bits.  */
  2056.             for (;;)
  2057.               {
  2058.                 if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
  2059.  
  2060.                 PATFETCH (c);
  2061.  
  2062.                 /* \ might escape characters inside [...] and [^...].  */
  2063.                 if ((syntax & RE_BACKSLASH_ESCAPE_IN_LISTS) && c == '\\')
  2064.                   {
  2065.                     if (p == pend) FREE_STACK_RETURN (REG_EESCAPE);
  2066.  
  2067.                     PATFETCH (c1);
  2068.                     SET_LIST_BIT (c1);
  2069.                     continue;
  2070.                   }
  2071.  
  2072.                 /* Could be the end of the bracket expression.  If it's
  2073.                    not (i.e., when the bracket expression is `[]' so
  2074.                    far), the ']' character bit gets set way below.  */
  2075.                 if (c == ']' && p != p1 + 1)
  2076.                   break;
  2077.  
  2078.                 /* Look ahead to see if it's a range when the last thing
  2079.                    was a character class.  */
  2080.                 if (had_char_class && c == '-' && *p != ']')
  2081.                   FREE_STACK_RETURN (REG_ERANGE);
  2082.  
  2083.                 /* Look ahead to see if it's a range when the last thing
  2084.                    was a character: if this is a hyphen not at the
  2085.                    beginning or the end of a list, then it's the range
  2086.                    operator.  */
  2087.                 if (c == '-'
  2088.                     && !(p - 2 >= pattern && p[-2] == '[')
  2089.                     && !(p - 3 >= pattern && p[-3] == '[' && p[-2] == '^')
  2090.                     && *p != ']')
  2091.                   {
  2092.                     reg_errcode_t ret
  2093.                       = compile_range (&p, pend, translate, syntax, b);
  2094.                     if (ret != REG_NOERROR) FREE_STACK_RETURN (ret);
  2095.                   }
  2096.  
  2097.                 else if (p[0] == '-' && p[1] != ']')
  2098.                   { /* This handles ranges made up of characters only.  */
  2099.                     reg_errcode_t ret;
  2100.  
  2101.             /* Move past the `-'.  */
  2102.                     PATFETCH (c1);
  2103.  
  2104.                     ret = compile_range (&p, pend, translate, syntax, b);
  2105.                     if (ret != REG_NOERROR) FREE_STACK_RETURN (ret);
  2106.                   }
  2107.  
  2108.                 /* See if we're at the beginning of a possible character
  2109.                    class.  */
  2110.  
  2111.                 else if (syntax & RE_CHAR_CLASSES && c == '[' && *p == ':')
  2112.                   { /* Leave room for the null.  */
  2113.                     char str[CHAR_CLASS_MAX_LENGTH + 1];
  2114.  
  2115.                     PATFETCH (c);
  2116.                     c1 = 0;
  2117.  
  2118.                     /* If pattern is `[[:'.  */
  2119.                     if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
  2120.  
  2121.                     for (;;)
  2122.                       {
  2123.                         PATFETCH (c);
  2124.                         if (c == ':' || c == ']' || p == pend
  2125.                             || c1 == CHAR_CLASS_MAX_LENGTH)
  2126.                           break;
  2127.                         str[c1++] = c;
  2128.                       }
  2129.                     str[c1] = '\0';
  2130.  
  2131.                     /* If isn't a word bracketed by `[:' and:`]':
  2132.                        undo the ending character, the letters, and leave
  2133.                        the leading `:' and `[' (but set bits for them).  */
  2134.                     if (c == ':' && *p == ']')
  2135.                       {
  2136.                         int ch;
  2137.                         boolean is_alnum = STREQ (str, "alnum");
  2138.                         boolean is_alpha = STREQ (str, "alpha");
  2139.                         boolean is_blank = STREQ (str, "blank");
  2140.                         boolean is_cntrl = STREQ (str, "cntrl");
  2141.                         boolean is_digit = STREQ (str, "digit");
  2142.                         boolean is_graph = STREQ (str, "graph");
  2143.                         boolean is_lower = STREQ (str, "lower");
  2144.                         boolean is_print = STREQ (str, "print");
  2145.                         boolean is_punct = STREQ (str, "punct");
  2146.                         boolean is_space = STREQ (str, "space");
  2147.                         boolean is_upper = STREQ (str, "upper");
  2148.                         boolean is_xdigit = STREQ (str, "xdigit");
  2149.  
  2150.                         if (!IS_CHAR_CLASS (str))
  2151.               FREE_STACK_RETURN (REG_ECTYPE);
  2152.  
  2153.                         /* Throw away the ] at the end of the character
  2154.                            class.  */
  2155.                         PATFETCH (c);
  2156.  
  2157.                         if (p == pend) FREE_STACK_RETURN (REG_EBRACK);
  2158.  
  2159.                         for (ch = 0; ch < 1 << BYTEWIDTH; ch++)
  2160.                           {
  2161.                 /* This was split into 3 if's to
  2162.                    avoid an arbitrary limit in some compiler.  */
  2163.                             if (   (is_alnum  && ISALNUM (ch))
  2164.                                 || (is_alpha  && ISALPHA (ch))
  2165.                                 || (is_blank  && ISBLANK (ch))
  2166.                                 || (is_cntrl  && ISCNTRL (ch)))
  2167.                   SET_LIST_BIT (ch);
  2168.                 if (   (is_digit  && ISDIGIT (ch))
  2169.                                 || (is_graph  && ISGRAPH (ch))
  2170.                                 || (is_lower  && ISLOWER (ch))
  2171.                                 || (is_print  && ISPRINT (ch)))
  2172.                   SET_LIST_BIT (ch);
  2173.                 if (   (is_punct  && ISPUNCT (ch))
  2174.                                 || (is_space  && ISSPACE (ch))
  2175.                                 || (is_upper  && ISUPPER (ch))
  2176.                                 || (is_xdigit && ISXDIGIT (ch)))
  2177.                   SET_LIST_BIT (ch);
  2178.                  if (   translate && (is_upper || is_lower)
  2179.                                 && (ISUPPER(ch) || ISLOWER(ch)))
  2180.                   SET_LIST_BIT (ch);
  2181.                           }
  2182.                         had_char_class = true;
  2183.                       }
  2184.                     else
  2185.                       {
  2186.                         c1++;
  2187.                         while (c1--)
  2188.                           PATUNFETCH;
  2189.                         SET_LIST_BIT ('[');
  2190.                         SET_LIST_BIT (':');
  2191.                         had_char_class = false;
  2192.                       }
  2193.                   }
  2194.                 else
  2195.                   {
  2196.                     had_char_class = false;
  2197.                     SET_LIST_BIT (c);
  2198.                   }
  2199.               }
  2200.  
  2201.             /* Discard any (non)matching list bytes that are all 0 at the
  2202.                end of the map.  Decrease the map-length byte too.  */
  2203.             while ((int) b[-1] > 0 && b[b[-1] - 1] == 0)
  2204.               b[-1]--;
  2205.             b += b[-1];
  2206.           }
  2207.           break;
  2208.  
  2209.  
  2210.     case '(':
  2211.           if (syntax & RE_NO_BK_PARENS)
  2212.             goto handle_open;
  2213.           else
  2214.             goto normal_char;
  2215.  
  2216.  
  2217.         case ')':
  2218.           if (syntax & RE_NO_BK_PARENS)
  2219.             goto handle_close;
  2220.           else
  2221.             goto normal_char;
  2222.  
  2223.  
  2224.         case '\n':
  2225.           if (syntax & RE_NEWLINE_ALT)
  2226.             goto handle_alt;
  2227.           else
  2228.             goto normal_char;
  2229.  
  2230.  
  2231.     case '|':
  2232.           if (syntax & RE_NO_BK_VBAR)
  2233.             goto handle_alt;
  2234.           else
  2235.             goto normal_char;
  2236.  
  2237.  
  2238.         case '{':
  2239.            if (syntax & RE_INTERVALS && syntax & RE_NO_BK_BRACES)
  2240.              goto handle_interval;
  2241.            else
  2242.              goto normal_char;
  2243.  
  2244.  
  2245.         case '\\':
  2246.           if (p == pend) FREE_STACK_RETURN (REG_EESCAPE);
  2247.  
  2248.           /* Do not translate the character after the \, so that we can
  2249.              distinguish, e.g., \B from \b, even if we normally would
  2250.              translate, e.g., B to b.  */
  2251.           PATFETCH_RAW (c);
  2252.  
  2253.           switch (c)
  2254.             {
  2255.             case '(':
  2256.               if (syntax & RE_NO_BK_PARENS)
  2257.                 goto normal_backslash;
  2258.  
  2259.             handle_open:
  2260.               bufp->re_nsub++;
  2261.               regnum++;
  2262.  
  2263.               if (COMPILE_STACK_FULL)
  2264.                 {
  2265.                   RETALLOC (compile_stack.stack, compile_stack.size << 1,
  2266.                             compile_stack_elt_t);
  2267.                   if (compile_stack.stack == NULL) return REG_ESPACE;
  2268.  
  2269.                   compile_stack.size <<= 1;
  2270.                 }
  2271.  
  2272.               /* These are the values to restore when we hit end of this
  2273.                  group.  They are all relative offsets, so that if the
  2274.                  whole pattern moves because of realloc, they will still
  2275.                  be valid.  */
  2276.               COMPILE_STACK_TOP.begalt_offset = begalt - bufp->buffer;
  2277.               COMPILE_STACK_TOP.fixup_alt_jump
  2278.                 = fixup_alt_jump ? fixup_alt_jump - bufp->buffer + 1 : 0;
  2279.               COMPILE_STACK_TOP.laststart_offset = b - bufp->buffer;
  2280.               COMPILE_STACK_TOP.regnum = regnum;
  2281.  
  2282.               /* We will eventually replace the 0 with the number of
  2283.                  groups inner to this one.  But do not push a
  2284.                  start_memory for groups beyond the last one we can
  2285.                  represent in the compiled pattern.  */
  2286.               if (regnum <= MAX_REGNUM)
  2287.                 {
  2288.                   COMPILE_STACK_TOP.inner_group_offset = b - bufp->buffer + 2;
  2289.                   BUF_PUSH_3 (start_memory, regnum, 0);
  2290.                 }
  2291.  
  2292.               compile_stack.avail++;
  2293.  
  2294.               fixup_alt_jump = 0;
  2295.               laststart = 0;
  2296.               begalt = b;
  2297.           /* If we've reached MAX_REGNUM groups, then this open
  2298.          won't actually generate any code, so we'll have to
  2299.          clear pending_exact explicitly.  */
  2300.           pending_exact = 0;
  2301.               break;
  2302.  
  2303.  
  2304.             case ')':
  2305.               if (syntax & RE_NO_BK_PARENS) goto normal_backslash;
  2306.  
  2307.               if (COMPILE_STACK_EMPTY)
  2308.                 if (syntax & RE_UNMATCHED_RIGHT_PAREN_ORD)
  2309.                   goto normal_backslash;
  2310.                 else
  2311.                   FREE_STACK_RETURN (REG_ERPAREN);
  2312.  
  2313.             handle_close:
  2314.               if (fixup_alt_jump)
  2315.                 { /* Push a dummy failure point at the end of the
  2316.                      alternative for a possible future
  2317.                      `pop_failure_jump' to pop.  See comments at
  2318.                      `push_dummy_failure' in `re_match_2'.  */
  2319.                   BUF_PUSH (push_dummy_failure);
  2320.  
  2321.                   /* We allocated space for this jump when we assigned
  2322.                      to `fixup_alt_jump', in the `handle_alt' case below.  */
  2323.                   STORE_JUMP (jump_past_alt, fixup_alt_jump, b - 1);
  2324.                 }
  2325.  
  2326.               /* See similar code for backslashed left paren above.  */
  2327.               if (COMPILE_STACK_EMPTY)
  2328.                 if (syntax & RE_UNMATCHED_RIGHT_PAREN_ORD)
  2329.                   goto normal_char;
  2330.                 else
  2331.                   FREE_STACK_RETURN (REG_ERPAREN);
  2332.  
  2333.               /* Since we just checked for an empty stack above, this
  2334.                  ``can't happen''.  */
  2335.               assert (compile_stack.avail != 0);
  2336.               {
  2337.                 /* We don't just want to restore into `regnum', because
  2338.                    later groups should continue to be numbered higher,
  2339.                    as in `(ab)c(de)' -- the second group is #2.  */
  2340.                 regnum_t this_group_regnum;
  2341.  
  2342.                 compile_stack.avail--;
  2343.                 begalt = bufp->buffer + COMPILE_STACK_TOP.begalt_offset;
  2344.                 fixup_alt_jump
  2345.                   = COMPILE_STACK_TOP.fixup_alt_jump
  2346.                     ? bufp->buffer + COMPILE_STACK_TOP.fixup_alt_jump - 1
  2347.                     : 0;
  2348.                 laststart = bufp->buffer + COMPILE_STACK_TOP.laststart_offset;
  2349.                 this_group_regnum = COMPILE_STACK_TOP.regnum;
  2350.         /* If we've reached MAX_REGNUM groups, then this open
  2351.            won't actually generate any code, so we'll have to
  2352.            clear pending_exact explicitly.  */
  2353.         pending_exact = 0;
  2354.  
  2355.                 /* We're at the end of the group, so now we know how many
  2356.                    groups were inside this one.  */
  2357.                 if (this_group_regnum <= MAX_REGNUM)
  2358.                   {
  2359.                     unsigned char *inner_group_loc
  2360.                       = bufp->buffer + COMPILE_STACK_TOP.inner_group_offset;
  2361.  
  2362.                     *inner_group_loc = regnum - this_group_regnum;
  2363.                     BUF_PUSH_3 (stop_memory, this_group_regnum,
  2364.                                 regnum - this_group_regnum);
  2365.                   }
  2366.               }
  2367.               break;
  2368.  
  2369.  
  2370.             case '|':                    /* `\|'.  */
  2371.               if (syntax & RE_LIMITED_OPS || syntax & RE_NO_BK_VBAR)
  2372.                 goto normal_backslash;
  2373.             handle_alt:
  2374.               if (syntax & RE_LIMITED_OPS)
  2375.                 goto normal_char;
  2376.  
  2377.               /* Insert before the previous alternative a jump which
  2378.                  jumps to this alternative if the former fails.  */
  2379.               GET_BUFFER_SPACE (3);
  2380.               INSERT_JUMP (on_failure_jump, begalt, b + 6);
  2381.               pending_exact = 0;
  2382.               b += 3;
  2383.  
  2384.               /* The alternative before this one has a jump after it
  2385.                  which gets executed if it gets matched.  Adjust that
  2386.                  jump so it will jump to this alternative's analogous
  2387.                  jump (put in below, which in turn will jump to the next
  2388.                  (if any) alternative's such jump, etc.).  The last such
  2389.                  jump jumps to the correct final destination.  A picture:
  2390.                           _____ _____
  2391.                           |   | |   |
  2392.                           |   v |   v
  2393.                          a | b   | c
  2394.  
  2395.                  If we are at `b', then fixup_alt_jump right now points to a
  2396.                  three-byte space after `a'.  We'll put in the jump, set
  2397.                  fixup_alt_jump to right after `b', and leave behind three
  2398.                  bytes which we'll fill in when we get to after `c'.  */
  2399.  
  2400.               if (fixup_alt_jump)
  2401.                 STORE_JUMP (jump_past_alt, fixup_alt_jump, b);
  2402.  
  2403.               /* Mark and leave space for a jump after this alternative,
  2404.                  to be filled in later either by next alternative or
  2405.                  when know we're at the end of a series of alternatives.  */
  2406.               fixup_alt_jump = b;
  2407.               GET_BUFFER_SPACE (3);
  2408.               b += 3;
  2409.  
  2410.               laststart = 0;
  2411.               begalt = b;
  2412.               break;
  2413.  
  2414.  
  2415.             case '{':
  2416.               /* If \{ is a literal.  */
  2417.               if (!(syntax & RE_INTERVALS)
  2418.                      /* If we're at `\{' and it's not the open-interval
  2419.                         operator.  */
  2420.                   || ((syntax & RE_INTERVALS) && (syntax & RE_NO_BK_BRACES))
  2421.                   || (p - 2 == pattern  &&  p == pend))
  2422.                 goto normal_backslash;
  2423.  
  2424.             handle_interval:
  2425.               {
  2426.                 /* If got here, then the syntax allows intervals.  */
  2427.  
  2428.                 /* At least (most) this many matches must be made.  */
  2429.                 int lower_bound = -1, upper_bound = -1;
  2430.  
  2431.                 beg_interval = p - 1;
  2432.  
  2433.                 if (p == pend)
  2434.                   {
  2435.                     if (syntax & RE_NO_BK_BRACES)
  2436.                       goto unfetch_interval;
  2437.                     else
  2438.                       FREE_STACK_RETURN (REG_EBRACE);
  2439.                   }
  2440.  
  2441.                 GET_UNSIGNED_NUMBER (lower_bound);
  2442.  
  2443.                 if (c == ',')
  2444.                   {
  2445.                     GET_UNSIGNED_NUMBER (upper_bound);
  2446.                     if (upper_bound < 0) upper_bound = RE_DUP_MAX;
  2447.                   }
  2448.                 else
  2449.                   /* Interval such as `{1}' => match exactly once. */
  2450.                   upper_bound = lower_bound;
  2451.  
  2452.                 if (lower_bound < 0 || upper_bound > RE_DUP_MAX
  2453.                     || lower_bound > upper_bound)
  2454.                   {
  2455.                     if (syntax & RE_NO_BK_BRACES)
  2456.                       goto unfetch_interval;
  2457.                     else
  2458.                       FREE_STACK_RETURN (REG_BADBR);
  2459.                   }
  2460.  
  2461.                 if (!(syntax & RE_NO_BK_BRACES))
  2462.                   {
  2463.                     if (c != '\\') FREE_STACK_RETURN (REG_EBRACE);
  2464.  
  2465.                     PATFETCH (c);
  2466.                   }
  2467.  
  2468.                 if (c != '}')
  2469.                   {
  2470.                     if (syntax & RE_NO_BK_BRACES)
  2471.                       goto unfetch_interval;
  2472.                     else
  2473.                       FREE_STACK_RETURN (REG_BADBR);
  2474.                   }
  2475.  
  2476.                 /* We just parsed a valid interval.  */
  2477.  
  2478.                 /* If it's invalid to have no preceding re.  */
  2479.                 if (!laststart)
  2480.                   {
  2481.                     if (syntax & RE_CONTEXT_INVALID_OPS)
  2482.                       FREE_STACK_RETURN (REG_BADRPT);
  2483.                     else if (syntax & RE_CONTEXT_INDEP_OPS)
  2484.                       laststart = b;
  2485.                     else
  2486.                       goto unfetch_interval;
  2487.                   }
  2488.  
  2489.                 /* If the upper bound is zero, don't want to succeed at
  2490.                    all; jump from `laststart' to `b + 3', which will be
  2491.                    the end of the buffer after we insert the jump.  */
  2492.                  if (upper_bound == 0)
  2493.                    {
  2494.                      GET_BUFFER_SPACE (3);
  2495.                      INSERT_JUMP (jump, laststart, b + 3);
  2496.                      b += 3;
  2497.                    }
  2498.  
  2499.                  /* Otherwise, we have a nontrivial interval.  When
  2500.                     we're all done, the pattern will look like:
  2501.                       set_number_at <jump count> <upper bound>
  2502.                       set_number_at <succeed_n count> <lower bound>
  2503.                       succeed_n <after jump addr> <succeed_n count>
  2504.                       <body of loop>
  2505.                       jump_n <succeed_n addr> <jump count>
  2506.                     (The upper bound and `jump_n' are omitted if
  2507.                     `upper_bound' is 1, though.)  */
  2508.                  else
  2509.                    { /* If the upper bound is > 1, we need to insert
  2510.                         more at the end of the loop.  */
  2511.                      unsigned nbytes = 10 + (upper_bound > 1) * 10;
  2512.  
  2513.                      GET_BUFFER_SPACE (nbytes);
  2514.  
  2515.                      /* Initialize lower bound of the `succeed_n', even
  2516.                         though it will be set during matching by its
  2517.                         attendant `set_number_at' (inserted next),
  2518.                         because `re_compile_fastmap' needs to know.
  2519.                         Jump to the `jump_n' we might insert below.  */
  2520.                      INSERT_JUMP2 (succeed_n, laststart,
  2521.                                    b + 5 + (upper_bound > 1) * 5,
  2522.                                    lower_bound);
  2523.                      b += 5;
  2524.  
  2525.                      /* Code to initialize the lower bound.  Insert
  2526.                         before the `succeed_n'.  The `5' is the last two
  2527.                         bytes of this `set_number_at', plus 3 bytes of
  2528.                         the following `succeed_n'.  */
  2529.                      insert_op2 (set_number_at, laststart, 5, lower_bound, b);
  2530.                      b += 5;
  2531.  
  2532.                      if (upper_bound > 1)
  2533.                        { /* More than one repetition is allowed, so
  2534.                             append a backward jump to the `succeed_n'
  2535.                             that starts this interval.
  2536.  
  2537.                             When we've reached this during matching,
  2538.                             we'll have matched the interval once, so
  2539.                             jump back only `upper_bound - 1' times.  */
  2540.                          STORE_JUMP2 (jump_n, b, laststart + 5,
  2541.                                       upper_bound - 1);
  2542.                          b += 5;
  2543.  
  2544.                          /* The location we want to set is the second
  2545.                             parameter of the `jump_n'; that is `b-2' as
  2546.                             an absolute address.  `laststart' will be
  2547.                             the `set_number_at' we're about to insert;
  2548.                             `laststart+3' the number to set, the source
  2549.                             for the relative address.  But we are
  2550.                             inserting into the middle of the pattern --
  2551.                             so everything is getting moved up by 5.
  2552.                             Conclusion: (b - 2) - (laststart + 3) + 5,
  2553.                             i.e., b - laststart.
  2554.  
  2555.                             We insert this at the beginning of the loop
  2556.                             so that if we fail during matching, we'll
  2557.                             reinitialize the bounds.  */
  2558.                          insert_op2 (set_number_at, laststart, b - laststart,
  2559.                                      upper_bound - 1, b);
  2560.                          b += 5;
  2561.                        }
  2562.                    }
  2563.                 pending_exact = 0;
  2564.                 beg_interval = NULL;
  2565.               }
  2566.               break;
  2567.  
  2568.             unfetch_interval:
  2569.               /* If an invalid interval, match the characters as literals.  */
  2570.                assert (beg_interval);
  2571.                p = beg_interval;
  2572.                beg_interval = NULL;
  2573.  
  2574.                /* normal_char and normal_backslash need `c'.  */
  2575.                PATFETCH (c);
  2576.  
  2577.                if (!(syntax & RE_NO_BK_BRACES))
  2578.                  {
  2579.                    if (p > pattern  &&  p[-1] == '\\')
  2580.                      goto normal_backslash;
  2581.                  }
  2582.                goto normal_char;
  2583.  
  2584. #ifdef emacs
  2585.             /* There is no way to specify the before_dot and after_dot
  2586.                operators.  rms says this is ok.  --karl  */
  2587.             case '=':
  2588.               BUF_PUSH (at_dot);
  2589.               break;
  2590.  
  2591.             case 's':
  2592.               laststart = b;
  2593.               PATFETCH (c);
  2594.               BUF_PUSH_2 (syntaxspec, syntax_spec_code[c]);
  2595.               break;
  2596.  
  2597.             case 'S':
  2598.               laststart = b;
  2599.               PATFETCH (c);
  2600.               BUF_PUSH_2 (notsyntaxspec, syntax_spec_code[c]);
  2601.               break;
  2602. #endif /* emacs */
  2603.  
  2604.  
  2605.             case 'w':
  2606.           if (re_syntax_options & RE_NO_GNU_OPS)
  2607.                goto normal_char;
  2608.               laststart = b;
  2609.               BUF_PUSH (wordchar);
  2610.               break;
  2611.  
  2612.  
  2613.             case 'W':
  2614.           if (re_syntax_options & RE_NO_GNU_OPS)
  2615.                goto normal_char;
  2616.               laststart = b;
  2617.               BUF_PUSH (notwordchar);
  2618.               break;
  2619.  
  2620.  
  2621.             case '<':
  2622.           if (re_syntax_options & RE_NO_GNU_OPS)
  2623.                goto normal_char;
  2624.               BUF_PUSH (wordbeg);
  2625.               break;
  2626.  
  2627.             case '>':
  2628.           if (re_syntax_options & RE_NO_GNU_OPS)
  2629.                goto normal_char;
  2630.               BUF_PUSH (wordend);
  2631.               break;
  2632.  
  2633.             case 'b':
  2634.           if (re_syntax_options & RE_NO_GNU_OPS)
  2635.                goto normal_char;
  2636.               BUF_PUSH (wordbound);
  2637.               break;
  2638.  
  2639.             case 'B':
  2640.           if (re_syntax_options & RE_NO_GNU_OPS)
  2641.                goto normal_char;
  2642.               BUF_PUSH (notwordbound);
  2643.               break;
  2644.  
  2645.             case '`':
  2646.           if (re_syntax_options & RE_NO_GNU_OPS)
  2647.                goto normal_char;
  2648.               BUF_PUSH (begbuf);
  2649.               break;
  2650.  
  2651.             case '\'':
  2652.           if (re_syntax_options & RE_NO_GNU_OPS)
  2653.                goto normal_char;
  2654.               BUF_PUSH (endbuf);
  2655.               break;
  2656.  
  2657.             case '1': case '2': case '3': case '4': case '5':
  2658.             case '6': case '7': case '8': case '9':
  2659.               if (syntax & RE_NO_BK_REFS)
  2660.                 goto normal_char;
  2661.  
  2662.               c1 = c - '0';
  2663.  
  2664.               if (c1 > regnum)
  2665.                 FREE_STACK_RETURN (REG_ESUBREG);
  2666.  
  2667.               /* Can't back reference to a subexpression if inside of it.  */
  2668.               if (group_in_compile_stack (compile_stack, (regnum_t)c1))
  2669.                 goto normal_char;
  2670.  
  2671.               laststart = b;
  2672.               BUF_PUSH_2 (duplicate, c1);
  2673.               break;
  2674.  
  2675.  
  2676.             case '+':
  2677.             case '?':
  2678.               if (syntax & RE_BK_PLUS_QM)
  2679.                 goto handle_plus;
  2680.               else
  2681.                 goto normal_backslash;
  2682.  
  2683.             default:
  2684.             normal_backslash:
  2685.               /* You might think it would be useful for \ to mean
  2686.                  not to translate; but if we don't translate it
  2687.                  it will never match anything.  */
  2688.               c = TRANSLATE (c);
  2689.               goto normal_char;
  2690.             }
  2691.           break;
  2692.  
  2693.  
  2694.     default:
  2695.         /* Expects the character in `c'.  */
  2696.     normal_char:
  2697.           /* If no exactn currently being built.  */
  2698.           if (!pending_exact
  2699.  
  2700.               /* If last exactn not at current position.  */
  2701.               || pending_exact + *pending_exact + 1 != b
  2702.  
  2703.               /* We have only one byte following the exactn for the count.  */
  2704.           || *pending_exact == (1 << BYTEWIDTH) - 1
  2705.  
  2706.               /* If followed by a repetition operator.  */
  2707.               || *p == '*' || *p == '^'
  2708.           || ((syntax & RE_BK_PLUS_QM)
  2709.           ? *p == '\\' && (p[1] == '+' || p[1] == '?')
  2710.           : (*p == '+' || *p == '?'))
  2711.           || ((syntax & RE_INTERVALS)
  2712.                   && ((syntax & RE_NO_BK_BRACES)
  2713.               ? *p == '{'
  2714.                       : (p[0] == '\\' && p[1] == '{'))))
  2715.         {
  2716.           /* Start building a new exactn.  */
  2717.  
  2718.               laststart = b;
  2719.  
  2720.           BUF_PUSH_2 (exactn, 0);
  2721.           pending_exact = b - 1;
  2722.             }
  2723.  
  2724.       BUF_PUSH (c);
  2725.           (*pending_exact)++;
  2726.       break;
  2727.         } /* switch (c) */
  2728.     } /* while p != pend */
  2729.  
  2730.  
  2731.   /* Through the pattern now.  */
  2732.  
  2733.   if (fixup_alt_jump)
  2734.     STORE_JUMP (jump_past_alt, fixup_alt_jump, b);
  2735.  
  2736.   if (!COMPILE_STACK_EMPTY)
  2737.     FREE_STACK_RETURN (REG_EPAREN);
  2738.  
  2739.   /* If we don't want backtracking, force success
  2740.      the first time we reach the end of the compiled pattern.  */
  2741.   if (syntax & RE_NO_POSIX_BACKTRACKING)
  2742.     BUF_PUSH (succeed);
  2743.  
  2744.   free (compile_stack.stack);
  2745.  
  2746.   /* We have succeeded; set the length of the buffer.  */
  2747.   bufp->used = b - bufp->buffer;
  2748.  
  2749. #ifdef DEBUG
  2750.   if (debug)
  2751.     {
  2752.       DEBUG_PRINT1 ("\nCompiled pattern: \n");
  2753.       print_compiled_pattern (bufp);
  2754.     }
  2755. #endif /* DEBUG */
  2756.  
  2757. #ifndef MATCH_MAY_ALLOCATE
  2758.   /* Initialize the failure stack to the largest possible stack.  This
  2759.      isn't necessary unless we're trying to avoid calling alloca in
  2760.      the search and match routines.  */
  2761.   {
  2762.     int num_regs = bufp->re_nsub + 1;
  2763.  
  2764.     /* Since DOUBLE_FAIL_STACK refuses to double only if the current size
  2765.        is strictly greater than re_max_failures, the largest possible stack
  2766.        is 2 * re_max_failures failure points.  */
  2767.     if (fail_stack.size < (2 * re_max_failures * MAX_FAILURE_ITEMS))
  2768.       {
  2769.     fail_stack.size = (2 * re_max_failures * MAX_FAILURE_ITEMS);
  2770.  
  2771. #ifdef emacs
  2772.     if (! fail_stack.stack)
  2773.       fail_stack.stack
  2774.         = (fail_stack_elt_t *) xmalloc (fail_stack.size
  2775.                         * sizeof (fail_stack_elt_t));
  2776.     else
  2777.       fail_stack.stack
  2778.         = (fail_stack_elt_t *) xrealloc (fail_stack.stack,
  2779.                          (fail_stack.size
  2780.                           * sizeof (fail_stack_elt_t)));
  2781. #else /* not emacs */
  2782.     if (! fail_stack.stack)
  2783.       fail_stack.stack
  2784.         = (fail_stack_elt_t *) malloc (fail_stack.size
  2785.                        * sizeof (fail_stack_elt_t));
  2786.     else
  2787.       fail_stack.stack
  2788.         = (fail_stack_elt_t *) realloc (fail_stack.stack,
  2789.                         (fail_stack.size
  2790.                          * sizeof (fail_stack_elt_t)));
  2791. #endif /* not emacs */
  2792.       }
  2793.  
  2794.     regex_grow_registers (num_regs);
  2795.   }
  2796. #endif /* not MATCH_MAY_ALLOCATE */
  2797.  
  2798.   return REG_NOERROR;
  2799. } /* regex_compile */
  2800.  
  2801. /* Subroutines for `regex_compile'.  */
  2802.  
  2803. /* Store OP at LOC followed by two-byte integer parameter ARG.  */
  2804.  
  2805. static void
  2806. store_op1 (op, loc, arg)
  2807.     re_opcode_t op;
  2808.     unsigned char *loc;
  2809.     int arg;
  2810. {
  2811.   *loc = (unsigned char) op;
  2812.   STORE_NUMBER (loc + 1, arg);
  2813. }
  2814.  
  2815.  
  2816. /* Like `store_op1', but for two two-byte parameters ARG1 and ARG2.  */
  2817.  
  2818. static void
  2819. store_op2 (op, loc, arg1, arg2)
  2820.     re_opcode_t op;
  2821.     unsigned char *loc;
  2822.     int arg1, arg2;
  2823. {
  2824.   *loc = (unsigned char) op;
  2825.   STORE_NUMBER (loc + 1, arg1);
  2826.   STORE_NUMBER (loc + 3, arg2);
  2827. }
  2828.  
  2829.  
  2830. /* Copy the bytes from LOC to END to open up three bytes of space at LOC
  2831.    for OP followed by two-byte integer parameter ARG.  */
  2832.  
  2833. static void
  2834. insert_op1 (op, loc, arg, end)
  2835.     re_opcode_t op;
  2836.     unsigned char *loc;
  2837.     int arg;
  2838.     unsigned char *end;
  2839. {
  2840.   register unsigned char *pfrom = end;
  2841.   register unsigned char *pto = end + 3;
  2842.  
  2843.   while (pfrom != loc)
  2844.     *--pto = *--pfrom;
  2845.  
  2846.   store_op1 (op, loc, arg);
  2847. }
  2848.  
  2849.  
  2850. /* Like `insert_op1', but for two two-byte parameters ARG1 and ARG2.  */
  2851.  
  2852. static void
  2853. insert_op2 (op, loc, arg1, arg2, end)
  2854.     re_opcode_t op;
  2855.     unsigned char *loc;
  2856.     int arg1, arg2;
  2857.     unsigned char *end;
  2858. {
  2859.   register unsigned char *pfrom = end;
  2860.   register unsigned char *pto = end + 5;
  2861.  
  2862.   while (pfrom != loc)
  2863.     *--pto = *--pfrom;
  2864.  
  2865.   store_op2 (op, loc, arg1, arg2);
  2866. }
  2867.  
  2868.  
  2869. /* P points to just after a ^ in PATTERN.  Return true if that ^ comes
  2870.    after an alternative or a begin-subexpression.  We assume there is at
  2871.    least one character before the ^.  */
  2872.  
  2873. static boolean
  2874. at_begline_loc_p (pattern, p, syntax)
  2875.     const char *pattern, *p;
  2876.     reg_syntax_t syntax;
  2877. {
  2878.   const char *prev = p - 2;
  2879.   boolean prev_prev_backslash = prev > pattern && prev[-1] == '\\';
  2880.  
  2881.   return
  2882.        /* After a subexpression?  */
  2883.        (*prev == '(' && (syntax & RE_NO_BK_PARENS || prev_prev_backslash))
  2884.        /* After an alternative?  */
  2885.     || (*prev == '|' && (syntax & RE_NO_BK_VBAR || prev_prev_backslash));
  2886. }
  2887.  
  2888.  
  2889. /* The dual of at_begline_loc_p.  This one is for $.  We assume there is
  2890.    at least one character after the $, i.e., `P < PEND'.  */
  2891.  
  2892. static boolean
  2893. at_endline_loc_p (p, pend, syntax)
  2894.     const char *p, *pend;
  2895.     reg_syntax_t syntax;
  2896. {
  2897.   const char *next = p;
  2898.   boolean next_backslash = *next == '\\';
  2899.   const char *next_next = p + 1 < pend ? p + 1 : 0;
  2900.  
  2901.   return
  2902.        /* Before a subexpression?  */
  2903.        (syntax & RE_NO_BK_PARENS ? *next == ')'
  2904.         : next_backslash && next_next && *next_next == ')')
  2905.        /* Before an alternative?  */
  2906.     || (syntax & RE_NO_BK_VBAR ? *next == '|'
  2907.         : next_backslash && next_next && *next_next == '|');
  2908. }
  2909.  
  2910.  
  2911. /* Returns true if REGNUM is in one of COMPILE_STACK's elements and
  2912.    false if it's not.  */
  2913.  
  2914. static boolean
  2915. group_in_compile_stack (compile_stack, regnum)
  2916.     compile_stack_type compile_stack;
  2917.     regnum_t regnum;
  2918. {
  2919.   int this_element;
  2920.  
  2921.   for (this_element = compile_stack.avail - 1;
  2922.        this_element >= 0;
  2923.        this_element--)
  2924.     if (compile_stack.stack[this_element].regnum == regnum)
  2925.       return true;
  2926.  
  2927.   return false;
  2928. }
  2929.  
  2930.  
  2931. /* Read the ending character of a range (in a bracket expression) from the
  2932.    uncompiled pattern *P_PTR (which ends at PEND).  We assume the
  2933.    starting character is in `P[-2]'.  (`P[-1]' is the character `-'.)
  2934.    Then we set the translation of all bits between the starting and
  2935.    ending characters (inclusive) in the compiled pattern B.
  2936.  
  2937.    Return an error code.
  2938.  
  2939.    We use these short variable names so we can use the same macros as
  2940.    `regex_compile' itself.  */
  2941.  
  2942. static reg_errcode_t
  2943. compile_range (p_ptr, pend, translate, syntax, b)
  2944.     const char **p_ptr, *pend;
  2945.     RE_TRANSLATE_TYPE translate;
  2946.     reg_syntax_t syntax;
  2947.     unsigned char *b;
  2948. {
  2949.   unsigned this_char;
  2950.  
  2951.   const char *p = *p_ptr;
  2952.   int range_start, range_end;
  2953.  
  2954.   if (p == pend)
  2955.     return REG_ERANGE;
  2956.  
  2957.   /* Even though the pattern is a signed `char *', we need to fetch
  2958.      with unsigned char *'s; if the high bit of the pattern character
  2959.      is set, the range endpoints will be negative if we fetch using a
  2960.      signed char *.
  2961.  
  2962.      We also want to fetch the endpoints without translating them; the
  2963.      appropriate translation is done in the bit-setting loop below.  */
  2964.   /* The SVR4 compiler on the 3B2 had trouble with unsigned const char *.  */
  2965.   range_start = ((const unsigned char *) p)[-2];
  2966.   range_end   = ((const unsigned char *) p)[0];
  2967.  
  2968.   /* Have to increment the pointer into the pattern string, so the
  2969.      caller isn't still at the ending character.  */
  2970.   (*p_ptr)++;
  2971.  
  2972.   /* If the start is after the end, the range is empty.  */
  2973.   if (range_start > range_end)
  2974.     return syntax & RE_NO_EMPTY_RANGES ? REG_ERANGE : REG_NOERROR;
  2975.  
  2976.   /* Here we see why `this_char' has to be larger than an `unsigned
  2977.      char' -- the range is inclusive, so if `range_end' == 0xff
  2978.      (assuming 8-bit characters), we would otherwise go into an infinite
  2979.      loop, since all characters <= 0xff.  */
  2980.   for (this_char = range_start; this_char <= range_end; this_char++)
  2981.     {
  2982.       SET_LIST_BIT (TRANSLATE (this_char));
  2983.     }
  2984.  
  2985.   return REG_NOERROR;
  2986. }
  2987.  
  2988. /* re_compile_fastmap computes a ``fastmap'' for the compiled pattern in
  2989.    BUFP.  A fastmap records which of the (1 << BYTEWIDTH) possible
  2990.    characters can start a string that matches the pattern.  This fastmap
  2991.    is used by re_search to skip quickly over impossible starting points.
  2992.  
  2993.    The caller must supply the address of a (1 << BYTEWIDTH)-byte data
  2994.    area as BUFP->fastmap.
  2995.  
  2996.    We set the `fastmap', `fastmap_accurate', and `can_be_null' fields in
  2997.    the pattern buffer.
  2998.  
  2999.    Returns 0 if we succeed, -2 if an internal error.   */
  3000.  
  3001. int
  3002. re_compile_fastmap (bufp)
  3003.      struct re_pattern_buffer *bufp;
  3004. {
  3005.   int j, k;
  3006. #ifdef MATCH_MAY_ALLOCATE
  3007.   fail_stack_type fail_stack;
  3008. #endif
  3009. #ifndef REGEX_MALLOC
  3010.   char *destination;
  3011. #endif
  3012.   /* We don't push any register information onto the failure stack.  */
  3013.   unsigned num_regs = 0;
  3014.  
  3015.   register char *fastmap = bufp->fastmap;
  3016.   unsigned char *pattern = bufp->buffer;
  3017.   unsigned char *p = pattern;
  3018.   register unsigned char *pend = pattern + bufp->used;
  3019.  
  3020. #ifdef REL_ALLOC
  3021.   /* This holds the pointer to the failure stack, when
  3022.      it is allocated relocatably.  */
  3023.   fail_stack_elt_t *failure_stack_ptr;
  3024. #endif
  3025.  
  3026.   /* Assume that each path through the pattern can be null until
  3027.      proven otherwise.  We set this false at the bottom of switch
  3028.      statement, to which we get only if a particular path doesn't
  3029.      match the empty string.  */
  3030.   boolean path_can_be_null = true;
  3031.  
  3032.   /* We aren't doing a `succeed_n' to begin with.  */
  3033.   boolean succeed_n_p = false;
  3034.  
  3035.   assert (fastmap != NULL && p != NULL);
  3036.  
  3037.   INIT_FAIL_STACK ();
  3038.   bzero (fastmap, 1 << BYTEWIDTH);  /* Assume nothing's valid.  */
  3039.   bufp->fastmap_accurate = 1;        /* It will be when we're done.  */
  3040.   bufp->can_be_null = 0;
  3041.  
  3042.   while (1)
  3043.     {
  3044.       if (p == pend || *p == succeed)
  3045.     {
  3046.       /* We have reached the (effective) end of pattern.  */
  3047.       if (!FAIL_STACK_EMPTY ())
  3048.         {
  3049.           bufp->can_be_null |= path_can_be_null;
  3050.  
  3051.           /* Reset for next path.  */
  3052.           path_can_be_null = true;
  3053.  
  3054.           p = fail_stack.stack[--fail_stack.avail].pointer;
  3055.  
  3056.           continue;
  3057.         }
  3058.       else
  3059.         break;
  3060.     }
  3061.  
  3062.       /* We should never be about to go beyond the end of the pattern.  */
  3063.       assert (p < pend);
  3064.  
  3065.       switch (SWITCH_ENUM_CAST ((re_opcode_t) *p++))
  3066.     {
  3067.  
  3068.         /* I guess the idea here is to simply not bother with a fastmap
  3069.            if a backreference is used, since it's too hard to figure out
  3070.            the fastmap for the corresponding group.  Setting
  3071.            `can_be_null' stops `re_search_2' from using the fastmap, so
  3072.            that is all we do.  */
  3073.     case duplicate:
  3074.       bufp->can_be_null = 1;
  3075.           goto done;
  3076.  
  3077.  
  3078.       /* Following are the cases which match a character.  These end
  3079.          with `break'.  */
  3080.  
  3081.     case exactn:
  3082.           fastmap[p[1]] = 1;
  3083.       break;
  3084.  
  3085.  
  3086.         case charset:
  3087.           for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--)
  3088.         if (p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH)))
  3089.               fastmap[j] = 1;
  3090.       break;
  3091.  
  3092.  
  3093.     case charset_not:
  3094.       /* Chars beyond end of map must be allowed.  */
  3095.       for (j = *p * BYTEWIDTH; j < (1 << BYTEWIDTH); j++)
  3096.             fastmap[j] = 1;
  3097.  
  3098.       for (j = *p++ * BYTEWIDTH - 1; j >= 0; j--)
  3099.         if (!(p[j / BYTEWIDTH] & (1 << (j % BYTEWIDTH))))
  3100.               fastmap[j] = 1;
  3101.           break;
  3102.  
  3103.  
  3104.     case wordchar:
  3105.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  3106.         if (SYNTAX (j) == Sword)
  3107.           fastmap[j] = 1;
  3108.       break;
  3109.  
  3110.  
  3111.     case notwordchar:
  3112.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  3113.         if (SYNTAX (j) != Sword)
  3114.           fastmap[j] = 1;
  3115.       break;
  3116.  
  3117.  
  3118.         case anychar:
  3119.       {
  3120.         int fastmap_newline = fastmap['\n'];
  3121.  
  3122.         /* `.' matches anything ...  */
  3123.         for (j = 0; j < (1 << BYTEWIDTH); j++)
  3124.           fastmap[j] = 1;
  3125.  
  3126.         /* ... except perhaps newline.  */
  3127.         if (!(bufp->syntax & RE_DOT_NEWLINE))
  3128.           fastmap['\n'] = fastmap_newline;
  3129.  
  3130.         /* Return if we have already set `can_be_null'; if we have,
  3131.            then the fastmap is irrelevant.  Something's wrong here.  */
  3132.         else if (bufp->can_be_null)
  3133.           goto done;
  3134.  
  3135.         /* Otherwise, have to check alternative paths.  */
  3136.         break;
  3137.       }
  3138.  
  3139. #ifdef emacs
  3140.         case syntaxspec:
  3141.       k = *p++;
  3142.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  3143.         if (SYNTAX (j) == (enum syntaxcode) k)
  3144.           fastmap[j] = 1;
  3145.       break;
  3146.  
  3147.  
  3148.     case notsyntaxspec:
  3149.       k = *p++;
  3150.       for (j = 0; j < (1 << BYTEWIDTH); j++)
  3151.         if (SYNTAX (j) != (enum syntaxcode) k)
  3152.           fastmap[j] = 1;
  3153.       break;
  3154.  
  3155.  
  3156.       /* All cases after this match the empty string.  These end with
  3157.          `continue'.  */
  3158.  
  3159.  
  3160.     case before_dot:
  3161.     case at_dot:
  3162.     case after_dot:
  3163.           continue;
  3164. #endif /* emacs */
  3165.  
  3166.  
  3167.         case no_op:
  3168.         case begline:
  3169.         case endline:
  3170.     case begbuf:
  3171.     case endbuf:
  3172.     case wordbound:
  3173.     case notwordbound:
  3174.     case wordbeg:
  3175.     case wordend:
  3176.         case push_dummy_failure:
  3177.           continue;
  3178.  
  3179.  
  3180.     case jump_n:
  3181.         case pop_failure_jump:
  3182.     case maybe_pop_jump:
  3183.     case jump:
  3184.         case jump_past_alt:
  3185.     case dummy_failure_jump:
  3186.           EXTRACT_NUMBER_AND_INCR (j, p);
  3187.       p += j;
  3188.       if (j > 0)
  3189.         continue;
  3190.  
  3191.           /* Jump backward implies we just went through the body of a
  3192.              loop and matched nothing.  Opcode jumped to should be
  3193.              `on_failure_jump' or `succeed_n'.  Just treat it like an
  3194.              ordinary jump.  For a * loop, it has pushed its failure
  3195.              point already; if so, discard that as redundant.  */
  3196.           if ((re_opcode_t) *p != on_failure_jump
  3197.           && (re_opcode_t) *p != succeed_n)
  3198.         continue;
  3199.  
  3200.           p++;
  3201.           EXTRACT_NUMBER_AND_INCR (j, p);
  3202.           p += j;
  3203.  
  3204.           /* If what's on the stack is where we are now, pop it.  */
  3205.           if (!FAIL_STACK_EMPTY ()
  3206.           && fail_stack.stack[fail_stack.avail - 1].pointer == p)
  3207.             fail_stack.avail--;
  3208.  
  3209.           continue;
  3210.  
  3211.  
  3212.         case on_failure_jump:
  3213.         case on_failure_keep_string_jump:
  3214.     handle_on_failure_jump:
  3215.           EXTRACT_NUMBER_AND_INCR (j, p);
  3216.  
  3217.           /* For some patterns, e.g., `(a?)?', `p+j' here points to the
  3218.              end of the pattern.  We don't want to push such a point,
  3219.              since when we restore it above, entering the switch will
  3220.              increment `p' past the end of the pattern.  We don't need
  3221.              to push such a point since we obviously won't find any more
  3222.              fastmap entries beyond `pend'.  Such a pattern can match
  3223.              the null string, though.  */
  3224.           if (p + j < pend)
  3225.             {
  3226.               if (!PUSH_PATTERN_OP (p + j, fail_stack))
  3227.         {
  3228.           RESET_FAIL_STACK ();
  3229.           return -2;
  3230.         }
  3231.             }
  3232.           else
  3233.             bufp->can_be_null = 1;
  3234.  
  3235.           if (succeed_n_p)
  3236.             {
  3237.               EXTRACT_NUMBER_AND_INCR (k, p);    /* Skip the n.  */
  3238.               succeed_n_p = false;
  3239.         }
  3240.  
  3241.           continue;
  3242.  
  3243.  
  3244.     case succeed_n:
  3245.           /* Get to the number of times to succeed.  */
  3246.           p += 2;
  3247.  
  3248.           /* Increment p past the n for when k != 0.  */
  3249.           EXTRACT_NUMBER_AND_INCR (k, p);
  3250.           if (k == 0)
  3251.         {
  3252.               p -= 4;
  3253.             succeed_n_p = true;  /* Spaghetti code alert.  */
  3254.               goto handle_on_failure_jump;
  3255.             }
  3256.           continue;
  3257.  
  3258.  
  3259.     case set_number_at:
  3260.           p += 4;
  3261.           continue;
  3262.  
  3263.  
  3264.     case start_memory:
  3265.         case stop_memory:
  3266.       p += 2;
  3267.       continue;
  3268.  
  3269.  
  3270.     default:
  3271.           abort (); /* We have listed all the cases.  */
  3272.         } /* switch *p++ */
  3273.  
  3274.       /* Getting here means we have found the possible starting
  3275.          characters for one path of the pattern -- and that the empty
  3276.          string does not match.  We need not follow this path further.
  3277.          Instead, look at the next alternative (remembered on the
  3278.          stack), or quit if no more.  The test at the top of the loop
  3279.          does these things.  */
  3280.       path_can_be_null = false;
  3281.       p = pend;
  3282.     } /* while p */
  3283.  
  3284.   /* Set `can_be_null' for the last path (also the first path, if the
  3285.      pattern is empty).  */
  3286.   bufp->can_be_null |= path_can_be_null;
  3287.  
  3288.  done:
  3289.   RESET_FAIL_STACK ();
  3290.   return 0;
  3291. } /* re_compile_fastmap */
  3292.  
  3293. /* Set REGS to hold NUM_REGS registers, storing them in STARTS and
  3294.    ENDS.  Subsequent matches using PATTERN_BUFFER and REGS will use
  3295.    this memory for recording register information.  STARTS and ENDS
  3296.    must be allocated using the malloc library routine, and must each
  3297.    be at least NUM_REGS * sizeof (regoff_t) bytes long.
  3298.  
  3299.    If NUM_REGS == 0, then subsequent matches should allocate their own
  3300.    register data.
  3301.  
  3302.    Unless this function is called, the first search or match using
  3303.    PATTERN_BUFFER will allocate its own register data, without
  3304.    freeing the old data.  */
  3305.  
  3306. void
  3307. re_set_registers (bufp, regs, num_regs, starts, ends)
  3308.     struct re_pattern_buffer *bufp;
  3309.     struct re_registers *regs;
  3310.     unsigned num_regs;
  3311.     regoff_t *starts, *ends;
  3312. {
  3313.   if (num_regs)
  3314.     {
  3315.       bufp->regs_allocated = REGS_REALLOCATE;
  3316.       regs->num_regs = num_regs;
  3317.       regs->start = starts;
  3318.       regs->end = ends;
  3319.     }
  3320.   else
  3321.     {
  3322.       bufp->regs_allocated = REGS_UNALLOCATED;
  3323.       regs->num_regs = 0;
  3324.       regs->start = regs->end = (regoff_t *) 0;
  3325.     }
  3326. }
  3327.  
  3328. /* Searching routines.  */
  3329.  
  3330. /* Like re_search_2, below, but only one string is specified, and
  3331.    doesn't let you say where to stop matching. */
  3332.  
  3333. int
  3334. re_search (bufp, string, size, startpos, range, regs)
  3335.      struct re_pattern_buffer *bufp;
  3336.      const char *string;
  3337.      int size, startpos, range;
  3338.      struct re_registers *regs;
  3339. {
  3340.   return re_search_2 (bufp, NULL, 0, string, size, startpos, range,
  3341.               regs, size);
  3342. }
  3343.  
  3344.  
  3345. /* Using the compiled pattern in BUFP->buffer, first tries to match the
  3346.    virtual concatenation of STRING1 and STRING2, starting first at index
  3347.    STARTPOS, then at STARTPOS + 1, and so on.
  3348.  
  3349.    STRING1 and STRING2 have length SIZE1 and SIZE2, respectively.
  3350.  
  3351.    RANGE is how far to scan while trying to match.  RANGE = 0 means try
  3352.    only at STARTPOS; in general, the last start tried is STARTPOS +
  3353.    RANGE.
  3354.  
  3355.    In REGS, return the indices of the virtual concatenation of STRING1
  3356.    and STRING2 that matched the entire BUFP->buffer and its contained
  3357.    subexpressions.
  3358.  
  3359.    Do not consider matching one past the index STOP in the virtual
  3360.    concatenation of STRING1 and STRING2.
  3361.  
  3362.    We return either the position in the strings at which the match was
  3363.    found, -1 if no match, or -2 if error (such as failure
  3364.    stack overflow).  */
  3365.  
  3366. int
  3367. re_search_2 (bufp, string1, size1, string2, size2, startpos, range, regs, stop)
  3368.      struct re_pattern_buffer *bufp;
  3369.      const char *string1, *string2;
  3370.      int size1, size2;
  3371.      int startpos;
  3372.      int range;
  3373.      struct re_registers *regs;
  3374.      int stop;
  3375. {
  3376.   int val;
  3377.   register char *fastmap = bufp->fastmap;
  3378.   register RE_TRANSLATE_TYPE translate = bufp->translate;
  3379.   int total_size = size1 + size2;
  3380.   int endpos = startpos + range;
  3381.  
  3382.   /* Check for out-of-range STARTPOS.  */
  3383.   if (startpos < 0 || startpos > total_size)
  3384.     return -1;
  3385.  
  3386.   /* Fix up RANGE if it might eventually take us outside
  3387.      the virtual concatenation of STRING1 and STRING2.
  3388.      Make sure we won't move STARTPOS below 0 or above TOTAL_SIZE.  */
  3389.   if (endpos < 0)
  3390.     range = 0 - startpos;
  3391.   else if (endpos > total_size)
  3392.     range = total_size - startpos;
  3393.  
  3394.   /* If the search isn't to be a backwards one, don't waste time in a
  3395.      search for a pattern that must be anchored.  */
  3396.   if (bufp->used > 0 && (re_opcode_t) bufp->buffer[0] == begbuf && range > 0)
  3397.     {
  3398.       if (startpos > 0)
  3399.     return -1;
  3400.       else
  3401.     range = 1;
  3402.     }
  3403.  
  3404. #ifdef emacs
  3405.   /* In a forward search for something that starts with \=.
  3406.      don't keep searching past point.  */
  3407.   if (bufp->used > 0 && (re_opcode_t) bufp->buffer[0] == at_dot && range > 0)
  3408.     {
  3409.       range = PT - startpos;
  3410.       if (range <= 0)
  3411.     return -1;
  3412.     }
  3413. #endif /* emacs */
  3414.  
  3415.   /* Update the fastmap now if not correct already.  */
  3416.   if (fastmap && !bufp->fastmap_accurate)
  3417.     if (re_compile_fastmap (bufp) == -2)
  3418.       return -2;
  3419.  
  3420.   /* Loop through the string, looking for a place to start matching.  */
  3421.   for (;;)
  3422.     {
  3423.       /* If a fastmap is supplied, skip quickly over characters that
  3424.          cannot be the start of a match.  If the pattern can match the
  3425.          null string, however, we don't need to skip characters; we want
  3426.          the first null string.  */
  3427.       if (fastmap && startpos < total_size && !bufp->can_be_null)
  3428.     {
  3429.       if (range > 0)    /* Searching forwards.  */
  3430.         {
  3431.           register const char *d;
  3432.           register int lim = 0;
  3433.           int irange = range;
  3434.  
  3435.               if (startpos < size1 && startpos + range >= size1)
  3436.                 lim = range - (size1 - startpos);
  3437.  
  3438.           d = (startpos >= size1 ? string2 - size1 : string1) + startpos;
  3439.  
  3440.               /* Written out as an if-else to avoid testing `translate'
  3441.                  inside the loop.  */
  3442.           if (translate)
  3443.                 while (range > lim
  3444.                        && !fastmap[(unsigned char)
  3445.                    translate[(unsigned char) *d++]])
  3446.                   range--;
  3447.           else
  3448.                 while (range > lim && !fastmap[(unsigned char) *d++])
  3449.                   range--;
  3450.  
  3451.           startpos += irange - range;
  3452.         }
  3453.       else                /* Searching backwards.  */
  3454.         {
  3455.           register char c = (size1 == 0 || startpos >= size1
  3456.                                  ? string2[startpos - size1]
  3457.                                  : string1[startpos]);
  3458.  
  3459.           if (!fastmap[(unsigned char) TRANSLATE (c)])
  3460.         goto advance;
  3461.         }
  3462.     }
  3463.  
  3464.       /* If can't match the null string, and that's all we have left, fail.  */
  3465.       if (range >= 0 && startpos == total_size && fastmap
  3466.           && !bufp->can_be_null)
  3467.     return -1;
  3468.  
  3469.       val = re_match_2_internal (bufp, string1, size1, string2, size2,
  3470.                  startpos, regs, stop);
  3471. #ifndef REGEX_MALLOC
  3472. #ifdef C_ALLOCA
  3473.       alloca (0);
  3474. #endif
  3475. #endif
  3476.  
  3477.       if (val >= 0)
  3478.     return startpos;
  3479.  
  3480.       if (val == -2)
  3481.     return -2;
  3482.  
  3483.     advance:
  3484.       if (!range)
  3485.         break;
  3486.       else if (range > 0)
  3487.         {
  3488.           range--;
  3489.           startpos++;
  3490.         }
  3491.       else
  3492.         {
  3493.           range++;
  3494.           startpos--;
  3495.         }
  3496.     }
  3497.   return -1;
  3498. } /* re_search_2 */
  3499.  
  3500. /* This converts PTR, a pointer into one of the search strings `string1'
  3501.    and `string2' into an offset from the beginning of that string.  */
  3502. #define POINTER_TO_OFFSET(ptr)            \
  3503.   (FIRST_STRING_P (ptr)                \
  3504.    ? ((regoff_t) ((ptr) - string1))        \
  3505.    : ((regoff_t) ((ptr) - string2 + size1)))
  3506.  
  3507. /* Macros for dealing with the split strings in re_match_2.  */
  3508.  
  3509. #define MATCHING_IN_FIRST_STRING  (dend == end_match_1)
  3510.  
  3511. /* Call before fetching a character with *d.  This switches over to
  3512.    string2 if necessary.  */
  3513. #define PREFETCH()                            \
  3514.   while (d == dend)                                \
  3515.     {                                    \
  3516.       /* End of string2 => fail.  */                    \
  3517.       if (dend == end_match_2)                         \
  3518.         goto fail;                            \
  3519.       /* End of string1 => advance to string2.  */             \
  3520.       d = string2;                                \
  3521.       dend = end_match_2;                        \
  3522.     }
  3523.  
  3524.  
  3525. /* Test if at very beginning or at very end of the virtual concatenation
  3526.    of `string1' and `string2'.  If only one string, it's `string2'.  */
  3527. #define AT_STRINGS_BEG(d) ((d) == (size1 ? string1 : string2) || !size2)
  3528. #define AT_STRINGS_END(d) ((d) == end2)
  3529.  
  3530.  
  3531. /* Test if D points to a character which is word-constituent.  We have
  3532.    two special cases to check for: if past the end of string1, look at
  3533.    the first character in string2; and if before the beginning of
  3534.    string2, look at the last character in string1.  */
  3535. #define WORDCHAR_P(d)                            \
  3536.   (SYNTAX ((d) == end1 ? *string2                    \
  3537.            : (d) == string2 - 1 ? *(end1 - 1) : *(d))            \
  3538.    == Sword)
  3539.  
  3540. /* Test if the character before D and the one at D differ with respect
  3541.    to being word-constituent.  */
  3542. #define AT_WORD_BOUNDARY(d)                        \
  3543.   (AT_STRINGS_BEG (d) || AT_STRINGS_END (d)                \
  3544.    || WORDCHAR_P (d - 1) != WORDCHAR_P (d))
  3545.  
  3546.  
  3547. /* Free everything we malloc.  */
  3548. #ifdef MATCH_MAY_ALLOCATE
  3549. #define FREE_VAR(var) if (var) REGEX_FREE (var); var = NULL
  3550. #define FREE_VARIABLES()                        \
  3551.   do {                                    \
  3552.     REGEX_FREE_STACK (fail_stack.stack);                \
  3553.     FREE_VAR (regstart);                        \
  3554.     FREE_VAR (regend);                            \
  3555.     FREE_VAR (old_regstart);                        \
  3556.     FREE_VAR (old_regend);                        \
  3557.     FREE_VAR (best_regstart);                        \
  3558.     FREE_VAR (best_regend);                        \
  3559.     FREE_VAR (reg_info);                        \
  3560.     FREE_VAR (reg_dummy);                        \
  3561.     FREE_VAR (reg_info_dummy);                        \
  3562.   } while (0)
  3563. #else
  3564. #define FREE_VARIABLES() ((void)0) /* Do nothing!  But inhibit gcc warning.  */
  3565. #endif /* not MATCH_MAY_ALLOCATE */
  3566.  
  3567. /* These values must meet several constraints.  They must not be valid
  3568.    register values; since we have a limit of 255 registers (because
  3569.    we use only one byte in the pattern for the register number), we can
  3570.    use numbers larger than 255.  They must differ by 1, because of
  3571.    NUM_FAILURE_ITEMS above.  And the value for the lowest register must
  3572.    be larger than the value for the highest register, so we do not try
  3573.    to actually save any registers when none are active.  */
  3574. #define NO_HIGHEST_ACTIVE_REG (1 << BYTEWIDTH)
  3575. #define NO_LOWEST_ACTIVE_REG (NO_HIGHEST_ACTIVE_REG + 1)
  3576.  
  3577. /* Matching routines.  */
  3578.  
  3579. #ifndef emacs   /* Emacs never uses this.  */
  3580. /* re_match is like re_match_2 except it takes only a single string.  */
  3581.  
  3582. int
  3583. re_match (bufp, string, size, pos, regs)
  3584.      struct re_pattern_buffer *bufp;
  3585.      const char *string;
  3586.      int size, pos;
  3587.      struct re_registers *regs;
  3588. {
  3589.   int result = re_match_2_internal (bufp, NULL, 0, string, size,
  3590.                     pos, regs, size);
  3591. #ifndef REGEX_MALLOC
  3592. #ifdef C_ALLOCA
  3593.   alloca (0);
  3594. #endif
  3595. #endif
  3596.   return result;
  3597. }
  3598. #endif /* not emacs */
  3599.  
  3600. static boolean group_match_null_string_p _RE_ARGS((unsigned char **p,
  3601.                            unsigned char *end,
  3602.                         register_info_type *reg_info));
  3603. static boolean alt_match_null_string_p _RE_ARGS((unsigned char *p,
  3604.                          unsigned char *end,
  3605.                       register_info_type *reg_info));
  3606. static boolean common_op_match_null_string_p _RE_ARGS((unsigned char **p,
  3607.                                unsigned char *end,
  3608.                         register_info_type *reg_info));
  3609. static int bcmp_translate _RE_ARGS((const char *s1, const char *s2,
  3610.                     int len, char *translate));
  3611.  
  3612. /* re_match_2 matches the compiled pattern in BUFP against the
  3613.    the (virtual) concatenation of STRING1 and STRING2 (of length SIZE1
  3614.    and SIZE2, respectively).  We start matching at POS, and stop
  3615.    matching at STOP.
  3616.  
  3617.    If REGS is non-null and the `no_sub' field of BUFP is nonzero, we
  3618.    store offsets for the substring each group matched in REGS.  See the
  3619.    documentation for exactly how many groups we fill.
  3620.  
  3621.    We return -1 if no match, -2 if an internal error (such as the
  3622.    failure stack overflowing).  Otherwise, we return the length of the
  3623.    matched substring.  */
  3624.  
  3625. int
  3626. re_match_2 (bufp, string1, size1, string2, size2, pos, regs, stop)
  3627.      struct re_pattern_buffer *bufp;
  3628.      const char *string1, *string2;
  3629.      int size1, size2;
  3630.      int pos;
  3631.      struct re_registers *regs;
  3632.      int stop;
  3633. {
  3634.   int result = re_match_2_internal (bufp, string1, size1, string2, size2,
  3635.                     pos, regs, stop);
  3636. #ifndef REGEX_MALLOC
  3637. #ifdef C_ALLOCA
  3638.   alloca (0);
  3639. #endif
  3640. #endif
  3641.   return result;
  3642. }
  3643.  
  3644. /* This is a separate function so that we can force an alloca cleanup
  3645.    afterwards.  */
  3646. static int
  3647. re_match_2_internal (bufp, string1, size1, string2, size2, pos, regs, stop)
  3648.      struct re_pattern_buffer *bufp;
  3649.      const char *string1, *string2;
  3650.      int size1, size2;
  3651.      int pos;
  3652.      struct re_registers *regs;
  3653.      int stop;
  3654. {
  3655.   /* General temporaries.  */
  3656.   int mcnt;
  3657.   unsigned char *p1;
  3658.  
  3659.   /* Just past the end of the corresponding string.  */
  3660.   const char *end1, *end2;
  3661.  
  3662.   /* Pointers into string1 and string2, just past the last characters in
  3663.      each to consider matching.  */
  3664.   const char *end_match_1, *end_match_2;
  3665.  
  3666.   /* Where we are in the data, and the end of the current string.  */
  3667.   const char *d, *dend;
  3668.  
  3669.   /* Where we are in the pattern, and the end of the pattern.  */
  3670.   unsigned char *p = bufp->buffer;
  3671.   register unsigned char *pend = p + bufp->used;
  3672.  
  3673.   /* Mark the opcode just after a start_memory, so we can test for an
  3674.      empty subpattern when we get to the stop_memory.  */
  3675.   unsigned char *just_past_start_mem = 0;
  3676.  
  3677.   /* We use this to map every character in the string.  */
  3678.   RE_TRANSLATE_TYPE translate = bufp->translate;
  3679.  
  3680.   /* Failure point stack.  Each place that can handle a failure further
  3681.      down the line pushes a failure point on this stack.  It consists of
  3682.      restart, regend, and reg_info for all registers corresponding to
  3683.      the subexpressions we're currently inside, plus the number of such
  3684.      registers, and, finally, two char *'s.  The first char * is where
  3685.      to resume scanning the pattern; the second one is where to resume
  3686.      scanning the strings.  If the latter is zero, the failure point is
  3687.      a ``dummy''; if a failure happens and the failure point is a dummy,
  3688.      it gets discarded and the next next one is tried.  */
  3689. #ifdef MATCH_MAY_ALLOCATE /* otherwise, this is global.  */
  3690.   fail_stack_type fail_stack;
  3691. #endif
  3692. #ifdef DEBUG
  3693.   static unsigned failure_id = 0;
  3694.   unsigned nfailure_points_pushed = 0, nfailure_points_popped = 0;
  3695. #endif
  3696.  
  3697. #ifdef REL_ALLOC
  3698.   /* This holds the pointer to the failure stack, when
  3699.      it is allocated relocatably.  */
  3700.   fail_stack_elt_t *failure_stack_ptr;
  3701. #endif
  3702.  
  3703.   /* We fill all the registers internally, independent of what we
  3704.      return, for use in backreferences.  The number here includes
  3705.      an element for register zero.  */
  3706.   size_t num_regs = bufp->re_nsub + 1;
  3707.  
  3708.   /* The currently active registers.  */
  3709.   active_reg_t lowest_active_reg = NO_LOWEST_ACTIVE_REG;
  3710.   active_reg_t highest_active_reg = NO_HIGHEST_ACTIVE_REG;
  3711.  
  3712.   /* Information on the contents of registers. These are pointers into
  3713.      the input strings; they record just what was matched (on this
  3714.      attempt) by a subexpression part of the pattern, that is, the
  3715.      regnum-th regstart pointer points to where in the pattern we began
  3716.      matching and the regnum-th regend points to right after where we
  3717.      stopped matching the regnum-th subexpression.  (The zeroth register
  3718.      keeps track of what the whole pattern matches.)  */
  3719. #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global.  */
  3720.   const char **regstart, **regend;
  3721. #endif
  3722.  
  3723.   /* If a group that's operated upon by a repetition operator fails to
  3724.      match anything, then the register for its start will need to be
  3725.      restored because it will have been set to wherever in the string we
  3726.      are when we last see its open-group operator.  Similarly for a
  3727.      register's end.  */
  3728. #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global.  */
  3729.   const char **old_regstart, **old_regend;
  3730. #endif
  3731.  
  3732.   /* The is_active field of reg_info helps us keep track of which (possibly
  3733.      nested) subexpressions we are currently in. The matched_something
  3734.      field of reg_info[reg_num] helps us tell whether or not we have
  3735.      matched any of the pattern so far this time through the reg_num-th
  3736.      subexpression.  These two fields get reset each time through any
  3737.      loop their register is in.  */
  3738. #ifdef MATCH_MAY_ALLOCATE /* otherwise, this is global.  */
  3739.   register_info_type *reg_info;
  3740. #endif
  3741.  
  3742.   /* The following record the register info as found in the above
  3743.      variables when we find a match better than any we've seen before.
  3744.      This happens as we backtrack through the failure points, which in
  3745.      turn happens only if we have not yet matched the entire string. */
  3746.   unsigned best_regs_set = false;
  3747. #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global.  */
  3748.   const char **best_regstart, **best_regend;
  3749. #endif
  3750.  
  3751.   /* Logically, this is `best_regend[0]'.  But we don't want to have to
  3752.      allocate space for that if we're not allocating space for anything
  3753.      else (see below).  Also, we never need info about register 0 for
  3754.      any of the other register vectors, and it seems rather a kludge to
  3755.      treat `best_regend' differently than the rest.  So we keep track of
  3756.      the end of the best match so far in a separate variable.  We
  3757.      initialize this to NULL so that when we backtrack the first time
  3758.      and need to test it, it's not garbage.  */
  3759.   const char *match_end = NULL;
  3760.  
  3761.   /* This helps SET_REGS_MATCHED avoid doing redundant work.  */
  3762.   int set_regs_matched_done = 0;
  3763.  
  3764.   /* Used when we pop values we don't care about.  */
  3765. #ifdef MATCH_MAY_ALLOCATE /* otherwise, these are global.  */
  3766.   const char **reg_dummy;
  3767.   register_info_type *reg_info_dummy;
  3768. #endif
  3769.  
  3770. #ifdef DEBUG
  3771.   /* Counts the total number of registers pushed.  */
  3772.   unsigned num_regs_pushed = 0;
  3773. #endif
  3774.  
  3775.   DEBUG_PRINT1 ("\n\nEntering re_match_2.\n");
  3776.  
  3777.   INIT_FAIL_STACK ();
  3778.  
  3779. #ifdef MATCH_MAY_ALLOCATE
  3780.   /* Do not bother to initialize all the register variables if there are
  3781.      no groups in the pattern, as it takes a fair amount of time.  If
  3782.      there are groups, we include space for register 0 (the whole
  3783.      pattern), even though we never use it, since it simplifies the
  3784.      array indexing.  We should fix this.  */
  3785.   if (bufp->re_nsub)
  3786.     {
  3787.       regstart = REGEX_TALLOC (num_regs, const char *);
  3788.       regend = REGEX_TALLOC (num_regs, const char *);
  3789.       old_regstart = REGEX_TALLOC (num_regs, const char *);
  3790.       old_regend = REGEX_TALLOC (num_regs, const char *);
  3791.       best_regstart = REGEX_TALLOC (num_regs, const char *);
  3792.       best_regend = REGEX_TALLOC (num_regs, const char *);
  3793.       reg_info = REGEX_TALLOC (num_regs, register_info_type);
  3794.       reg_dummy = REGEX_TALLOC (num_regs, const char *);
  3795.       reg_info_dummy = REGEX_TALLOC (num_regs, register_info_type);
  3796.  
  3797.       if (!(regstart && regend && old_regstart && old_regend && reg_info
  3798.             && best_regstart && best_regend && reg_dummy && reg_info_dummy))
  3799.         {
  3800.           FREE_VARIABLES ();
  3801.           return -2;
  3802.         }
  3803.     }
  3804.   else
  3805.     {
  3806.       /* We must initialize all our variables to NULL, so that
  3807.          `FREE_VARIABLES' doesn't try to free them.  */
  3808.       regstart = regend = old_regstart = old_regend = best_regstart
  3809.         = best_regend = reg_dummy = NULL;
  3810.       reg_info = reg_info_dummy = (register_info_type *) NULL;
  3811.     }
  3812. #endif /* MATCH_MAY_ALLOCATE */
  3813.  
  3814.   /* The starting position is bogus.  */
  3815.   if (pos < 0 || pos > size1 + size2)
  3816.     {
  3817.       FREE_VARIABLES ();
  3818.       return -1;
  3819.     }
  3820.  
  3821.   /* Initialize subexpression text positions to -1 to mark ones that no
  3822.      start_memory/stop_memory has been seen for. Also initialize the
  3823.      register information struct.  */
  3824.   for (mcnt = 1; mcnt < num_regs; mcnt++)
  3825.     {
  3826.       regstart[mcnt] = regend[mcnt]
  3827.         = old_regstart[mcnt] = old_regend[mcnt] = REG_UNSET_VALUE;
  3828.  
  3829.       REG_MATCH_NULL_STRING_P (reg_info[mcnt]) = MATCH_NULL_UNSET_VALUE;
  3830.       IS_ACTIVE (reg_info[mcnt]) = 0;
  3831.       MATCHED_SOMETHING (reg_info[mcnt]) = 0;
  3832.       EVER_MATCHED_SOMETHING (reg_info[mcnt]) = 0;
  3833.     }
  3834.  
  3835.   /* We move `string1' into `string2' if the latter's empty -- but not if
  3836.      `string1' is null.  */
  3837.   if (size2 == 0 && string1 != NULL)
  3838.     {
  3839.       string2 = string1;
  3840.       size2 = size1;
  3841.       string1 = 0;
  3842.       size1 = 0;
  3843.     }
  3844.   end1 = string1 + size1;
  3845.   end2 = string2 + size2;
  3846.  
  3847.   /* Compute where to stop matching, within the two strings.  */
  3848.   if (stop <= size1)
  3849.     {
  3850.       end_match_1 = string1 + stop;
  3851.       end_match_2 = string2;
  3852.     }
  3853.   else
  3854.     {
  3855.       end_match_1 = end1;
  3856.       end_match_2 = string2 + stop - size1;
  3857.     }
  3858.  
  3859.   /* `p' scans through the pattern as `d' scans through the data.
  3860.      `dend' is the end of the input string that `d' points within.  `d'
  3861.      is advanced into the following input string whenever necessary, but
  3862.      this happens before fetching; therefore, at the beginning of the
  3863.      loop, `d' can be pointing at the end of a string, but it cannot
  3864.      equal `string2'.  */
  3865.   if (size1 > 0 && pos <= size1)
  3866.     {
  3867.       d = string1 + pos;
  3868.       dend = end_match_1;
  3869.     }
  3870.   else
  3871.     {
  3872.       d = string2 + pos - size1;
  3873.       dend = end_match_2;
  3874.     }
  3875.  
  3876.   DEBUG_PRINT1 ("The compiled pattern is: ");
  3877.   DEBUG_PRINT_COMPILED_PATTERN (bufp, p, pend);
  3878.   DEBUG_PRINT1 ("The string to match is: `");
  3879.   DEBUG_PRINT_DOUBLE_STRING (d, string1, size1, string2, size2);
  3880.   DEBUG_PRINT1 ("'\n");
  3881.  
  3882.   /* This loops over pattern commands.  It exits by returning from the
  3883.      function if the match is complete, or it drops through if the match
  3884.      fails at this starting point in the input data.  */
  3885.   for (;;)
  3886.     {
  3887.       DEBUG_PRINT2 ("\n0x%x: ", p);
  3888.  
  3889.       if (p == pend)
  3890.     { /* End of pattern means we might have succeeded.  */
  3891.           DEBUG_PRINT1 ("end of pattern ... ");
  3892.  
  3893.       /* If we haven't matched the entire string, and we want the
  3894.              longest match, try backtracking.  */
  3895.           if (d != end_match_2)
  3896.         {
  3897.           /* 1 if this match ends in the same string (string1 or string2)
  3898.          as the best previous match.  */
  3899.           boolean same_str_p = (FIRST_STRING_P (match_end)
  3900.                     == MATCHING_IN_FIRST_STRING);
  3901.           /* 1 if this match is the best seen so far.  */
  3902.           boolean best_match_p;
  3903.  
  3904.           /* AIX compiler got confused when this was combined
  3905.          with the previous declaration.  */
  3906.           if (same_str_p)
  3907.         best_match_p = d > match_end;
  3908.           else
  3909.         best_match_p = !MATCHING_IN_FIRST_STRING;
  3910.  
  3911.               DEBUG_PRINT1 ("backtracking.\n");
  3912.  
  3913.               if (!FAIL_STACK_EMPTY ())
  3914.                 { /* More failure points to try.  */
  3915.  
  3916.                   /* If exceeds best match so far, save it.  */
  3917.                   if (!best_regs_set || best_match_p)
  3918.                     {
  3919.                       best_regs_set = true;
  3920.                       match_end = d;
  3921.  
  3922.                       DEBUG_PRINT1 ("\nSAVING match as best so far.\n");
  3923.  
  3924.                       for (mcnt = 1; mcnt < num_regs; mcnt++)
  3925.                         {
  3926.                           best_regstart[mcnt] = regstart[mcnt];
  3927.                           best_regend[mcnt] = regend[mcnt];
  3928.                         }
  3929.                     }
  3930.                   goto fail;
  3931.                 }
  3932.  
  3933.               /* If no failure points, don't restore garbage.  And if
  3934.                  last match is real best match, don't restore second
  3935.                  best one. */
  3936.               else if (best_regs_set && !best_match_p)
  3937.                 {
  3938.               restore_best_regs:
  3939.                   /* Restore best match.  It may happen that `dend ==
  3940.                      end_match_1' while the restored d is in string2.
  3941.                      For example, the pattern `x.*y.*z' against the
  3942.                      strings `x-' and `y-z-', if the two strings are
  3943.                      not consecutive in memory.  */
  3944.                   DEBUG_PRINT1 ("Restoring best registers.\n");
  3945.  
  3946.                   d = match_end;
  3947.                   dend = ((d >= string1 && d <= end1)
  3948.                    ? end_match_1 : end_match_2);
  3949.  
  3950.           for (mcnt = 1; mcnt < num_regs; mcnt++)
  3951.             {
  3952.               regstart[mcnt] = best_regstart[mcnt];
  3953.               regend[mcnt] = best_regend[mcnt];
  3954.             }
  3955.                 }
  3956.             } /* d != end_match_2 */
  3957.  
  3958.     succeed_label:
  3959.           DEBUG_PRINT1 ("Accepting match.\n");
  3960.  
  3961.           /* If caller wants register contents data back, do it.  */
  3962.           if (regs && !bufp->no_sub)
  3963.         {
  3964.               /* Have the register data arrays been allocated?  */
  3965.               if (bufp->regs_allocated == REGS_UNALLOCATED)
  3966.                 { /* No.  So allocate them with malloc.  We need one
  3967.                      extra element beyond `num_regs' for the `-1' marker
  3968.                      GNU code uses.  */
  3969.                   regs->num_regs = MAX (RE_NREGS, num_regs + 1);
  3970.                   regs->start = TALLOC (regs->num_regs, regoff_t);
  3971.                   regs->end = TALLOC (regs->num_regs, regoff_t);
  3972.                   if (regs->start == NULL || regs->end == NULL)
  3973.             {
  3974.               FREE_VARIABLES ();
  3975.               return -2;
  3976.             }
  3977.                   bufp->regs_allocated = REGS_REALLOCATE;
  3978.                 }
  3979.               else if (bufp->regs_allocated == REGS_REALLOCATE)
  3980.                 { /* Yes.  If we need more elements than were already
  3981.                      allocated, reallocate them.  If we need fewer, just
  3982.                      leave it alone.  */
  3983.                   if (regs->num_regs < num_regs + 1)
  3984.                     {
  3985.                       regs->num_regs = num_regs + 1;
  3986.                       RETALLOC (regs->start, regs->num_regs, regoff_t);
  3987.                       RETALLOC (regs->end, regs->num_regs, regoff_t);
  3988.                       if (regs->start == NULL || regs->end == NULL)
  3989.             {
  3990.               FREE_VARIABLES ();
  3991.               return -2;
  3992.             }
  3993.                     }
  3994.                 }
  3995.               else
  3996.         {
  3997.           /* These braces fend off a "empty body in an else-statement"
  3998.              warning under GCC when assert expands to nothing.  */
  3999.           assert (bufp->regs_allocated == REGS_FIXED);
  4000.         }
  4001.  
  4002.               /* Convert the pointer data in `regstart' and `regend' to
  4003.                  indices.  Register zero has to be set differently,
  4004.                  since we haven't kept track of any info for it.  */
  4005.               if (regs->num_regs > 0)
  4006.                 {
  4007.                   regs->start[0] = pos;
  4008.                   regs->end[0] = (MATCHING_IN_FIRST_STRING
  4009.                   ? ((regoff_t) (d - string1))
  4010.                       : ((regoff_t) (d - string2 + size1)));
  4011.                 }
  4012.  
  4013.               /* Go through the first `min (num_regs, regs->num_regs)'
  4014.                  registers, since that is all we initialized.  */
  4015.           for (mcnt = 1; mcnt < MIN (num_regs, regs->num_regs); mcnt++)
  4016.         {
  4017.                   if (REG_UNSET (regstart[mcnt]) || REG_UNSET (regend[mcnt]))
  4018.                     regs->start[mcnt] = regs->end[mcnt] = -1;
  4019.                   else
  4020.                     {
  4021.               regs->start[mcnt]
  4022.             = (regoff_t) POINTER_TO_OFFSET (regstart[mcnt]);
  4023.                       regs->end[mcnt]
  4024.             = (regoff_t) POINTER_TO_OFFSET (regend[mcnt]);
  4025.                     }
  4026.         }
  4027.  
  4028.               /* If the regs structure we return has more elements than
  4029.                  were in the pattern, set the extra elements to -1.  If
  4030.                  we (re)allocated the registers, this is the case,
  4031.                  because we always allocate enough to have at least one
  4032.                  -1 at the end.  */
  4033.               for (mcnt = num_regs; mcnt < regs->num_regs; mcnt++)
  4034.                 regs->start[mcnt] = regs->end[mcnt] = -1;
  4035.         } /* regs && !bufp->no_sub */
  4036.  
  4037.           DEBUG_PRINT4 ("%u failure points pushed, %u popped (%u remain).\n",
  4038.                         nfailure_points_pushed, nfailure_points_popped,
  4039.                         nfailure_points_pushed - nfailure_points_popped);
  4040.           DEBUG_PRINT2 ("%u registers pushed.\n", num_regs_pushed);
  4041.  
  4042.           mcnt = d - pos - (MATCHING_IN_FIRST_STRING
  4043.                 ? string1
  4044.                 : string2 - size1);
  4045.  
  4046.           DEBUG_PRINT2 ("Returning %d from re_match_2.\n", mcnt);
  4047.  
  4048.           FREE_VARIABLES ();
  4049.           return mcnt;
  4050.         }
  4051.  
  4052.       /* Otherwise match next pattern command.  */
  4053.       switch (SWITCH_ENUM_CAST ((re_opcode_t) *p++))
  4054.     {
  4055.         /* Ignore these.  Used to ignore the n of succeed_n's which
  4056.            currently have n == 0.  */
  4057.         case no_op:
  4058.           DEBUG_PRINT1 ("EXECUTING no_op.\n");
  4059.           break;
  4060.  
  4061.     case succeed:
  4062.           DEBUG_PRINT1 ("EXECUTING succeed.\n");
  4063.       goto succeed_label;
  4064.  
  4065.         /* Match the next n pattern characters exactly.  The following
  4066.            byte in the pattern defines n, and the n bytes after that
  4067.            are the characters to match.  */
  4068.     case exactn:
  4069.       mcnt = *p++;
  4070.           DEBUG_PRINT2 ("EXECUTING exactn %d.\n", mcnt);
  4071.  
  4072.           /* This is written out as an if-else so we don't waste time
  4073.              testing `translate' inside the loop.  */
  4074.           if (translate)
  4075.         {
  4076.           do
  4077.         {
  4078.           PREFETCH ();
  4079.           if ((unsigned char) translate[(unsigned char) *d++]
  4080.               != (unsigned char) *p++)
  4081.                     goto fail;
  4082.         }
  4083.           while (--mcnt);
  4084.         }
  4085.       else
  4086.         {
  4087.           do
  4088.         {
  4089.           PREFETCH ();
  4090.           if (*d++ != (char) *p++) goto fail;
  4091.         }
  4092.           while (--mcnt);
  4093.         }
  4094.       SET_REGS_MATCHED ();
  4095.           break;
  4096.  
  4097.  
  4098.         /* Match any character except possibly a newline or a null.  */
  4099.     case anychar:
  4100.           DEBUG_PRINT1 ("EXECUTING anychar.\n");
  4101.  
  4102.           PREFETCH ();
  4103.  
  4104.           if ((!(bufp->syntax & RE_DOT_NEWLINE) && TRANSLATE (*d) == '\n')
  4105.               || (bufp->syntax & RE_DOT_NOT_NULL && TRANSLATE (*d) == '\000'))
  4106.         goto fail;
  4107.  
  4108.           SET_REGS_MATCHED ();
  4109.           DEBUG_PRINT2 ("  Matched `%d'.\n", *d);
  4110.           d++;
  4111.       break;
  4112.  
  4113.  
  4114.     case charset:
  4115.     case charset_not:
  4116.       {
  4117.         register unsigned char c;
  4118.         boolean not = (re_opcode_t) *(p - 1) == charset_not;
  4119.  
  4120.             DEBUG_PRINT2 ("EXECUTING charset%s.\n", not ? "_not" : "");
  4121.  
  4122.         PREFETCH ();
  4123.         c = TRANSLATE (*d); /* The character to match.  */
  4124.  
  4125.             /* Cast to `unsigned' instead of `unsigned char' in case the
  4126.                bit list is a full 32 bytes long.  */
  4127.         if (c < (unsigned) (*p * BYTEWIDTH)
  4128.         && p[1 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
  4129.           not = !not;
  4130.  
  4131.         p += 1 + *p;
  4132.  
  4133.         if (!not) goto fail;
  4134.  
  4135.         SET_REGS_MATCHED ();
  4136.             d++;
  4137.         break;
  4138.       }
  4139.  
  4140.  
  4141.         /* The beginning of a group is represented by start_memory.
  4142.            The arguments are the register number in the next byte, and the
  4143.            number of groups inner to this one in the next.  The text
  4144.            matched within the group is recorded (in the internal
  4145.            registers data structure) under the register number.  */
  4146.         case start_memory:
  4147.       DEBUG_PRINT3 ("EXECUTING start_memory %d (%d):\n", *p, p[1]);
  4148.  
  4149.           /* Find out if this group can match the empty string.  */
  4150.       p1 = p;        /* To send to group_match_null_string_p.  */
  4151.  
  4152.           if (REG_MATCH_NULL_STRING_P (reg_info[*p]) == MATCH_NULL_UNSET_VALUE)
  4153.             REG_MATCH_NULL_STRING_P (reg_info[*p])
  4154.               = group_match_null_string_p (&p1, pend, reg_info);
  4155.  
  4156.           /* Save the position in the string where we were the last time
  4157.              we were at this open-group operator in case the group is
  4158.              operated upon by a repetition operator, e.g., with `(a*)*b'
  4159.              against `ab'; then we want to ignore where we are now in
  4160.              the string in case this attempt to match fails.  */
  4161.           old_regstart[*p] = REG_MATCH_NULL_STRING_P (reg_info[*p])
  4162.                              ? REG_UNSET (regstart[*p]) ? d : regstart[*p]
  4163.                              : regstart[*p];
  4164.       DEBUG_PRINT2 ("  old_regstart: %d\n",
  4165.              POINTER_TO_OFFSET (old_regstart[*p]));
  4166.  
  4167.           regstart[*p] = d;
  4168.       DEBUG_PRINT2 ("  regstart: %d\n", POINTER_TO_OFFSET (regstart[*p]));
  4169.  
  4170.           IS_ACTIVE (reg_info[*p]) = 1;
  4171.           MATCHED_SOMETHING (reg_info[*p]) = 0;
  4172.  
  4173.       /* Clear this whenever we change the register activity status.  */
  4174.       set_regs_matched_done = 0;
  4175.  
  4176.           /* This is the new highest active register.  */
  4177.           highest_active_reg = *p;
  4178.  
  4179.           /* If nothing was active before, this is the new lowest active
  4180.              register.  */
  4181.           if (lowest_active_reg == NO_LOWEST_ACTIVE_REG)
  4182.             lowest_active_reg = *p;
  4183.  
  4184.           /* Move past the register number and inner group count.  */
  4185.           p += 2;
  4186.       just_past_start_mem = p;
  4187.  
  4188.           break;
  4189.  
  4190.  
  4191.         /* The stop_memory opcode represents the end of a group.  Its
  4192.            arguments are the same as start_memory's: the register
  4193.            number, and the number of inner groups.  */
  4194.     case stop_memory:
  4195.       DEBUG_PRINT3 ("EXECUTING stop_memory %d (%d):\n", *p, p[1]);
  4196.  
  4197.           /* We need to save the string position the last time we were at
  4198.              this close-group operator in case the group is operated
  4199.              upon by a repetition operator, e.g., with `((a*)*(b*)*)*'
  4200.              against `aba'; then we want to ignore where we are now in
  4201.              the string in case this attempt to match fails.  */
  4202.           old_regend[*p] = REG_MATCH_NULL_STRING_P (reg_info[*p])
  4203.                            ? REG_UNSET (regend[*p]) ? d : regend[*p]
  4204.                : regend[*p];
  4205.       DEBUG_PRINT2 ("      old_regend: %d\n",
  4206.              POINTER_TO_OFFSET (old_regend[*p]));
  4207.  
  4208.           regend[*p] = d;
  4209.       DEBUG_PRINT2 ("      regend: %d\n", POINTER_TO_OFFSET (regend[*p]));
  4210.  
  4211.           /* This register isn't active anymore.  */
  4212.           IS_ACTIVE (reg_info[*p]) = 0;
  4213.  
  4214.       /* Clear this whenever we change the register activity status.  */
  4215.       set_regs_matched_done = 0;
  4216.  
  4217.           /* If this was the only register active, nothing is active
  4218.              anymore.  */
  4219.           if (lowest_active_reg == highest_active_reg)
  4220.             {
  4221.               lowest_active_reg = NO_LOWEST_ACTIVE_REG;
  4222.               highest_active_reg = NO_HIGHEST_ACTIVE_REG;
  4223.             }
  4224.           else
  4225.             { /* We must scan for the new highest active register, since
  4226.                  it isn't necessarily one less than now: consider
  4227.                  (a(b)c(d(e)f)g).  When group 3 ends, after the f), the
  4228.                  new highest active register is 1.  */
  4229.               unsigned char r = *p - 1;
  4230.               while (r > 0 && !IS_ACTIVE (reg_info[r]))
  4231.                 r--;
  4232.  
  4233.               /* If we end up at register zero, that means that we saved
  4234.                  the registers as the result of an `on_failure_jump', not
  4235.                  a `start_memory', and we jumped to past the innermost
  4236.                  `stop_memory'.  For example, in ((.)*) we save
  4237.                  registers 1 and 2 as a result of the *, but when we pop
  4238.                  back to the second ), we are at the stop_memory 1.
  4239.                  Thus, nothing is active.  */
  4240.           if (r == 0)
  4241.                 {
  4242.                   lowest_active_reg = NO_LOWEST_ACTIVE_REG;
  4243.                   highest_active_reg = NO_HIGHEST_ACTIVE_REG;
  4244.                 }
  4245.               else
  4246.                 highest_active_reg = r;
  4247.             }
  4248.  
  4249.           /* If just failed to match something this time around with a
  4250.              group that's operated on by a repetition operator, try to
  4251.              force exit from the ``loop'', and restore the register
  4252.              information for this group that we had before trying this
  4253.              last match.  */
  4254.           if ((!MATCHED_SOMETHING (reg_info[*p])
  4255.                || just_past_start_mem == p - 1)
  4256.           && (p + 2) < pend)
  4257.             {
  4258.               boolean is_a_jump_n = false;
  4259.  
  4260.               p1 = p + 2;
  4261.               mcnt = 0;
  4262.               switch ((re_opcode_t) *p1++)
  4263.                 {
  4264.                   case jump_n:
  4265.             is_a_jump_n = true;
  4266.                   case pop_failure_jump:
  4267.           case maybe_pop_jump:
  4268.           case jump:
  4269.           case dummy_failure_jump:
  4270.                     EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  4271.             if (is_a_jump_n)
  4272.               p1 += 2;
  4273.                     break;
  4274.  
  4275.                   default:
  4276.                     /* do nothing */ ;
  4277.                 }
  4278.           p1 += mcnt;
  4279.  
  4280.               /* If the next operation is a jump backwards in the pattern
  4281.              to an on_failure_jump right before the start_memory
  4282.                  corresponding to this stop_memory, exit from the loop
  4283.                  by forcing a failure after pushing on the stack the
  4284.                  on_failure_jump's jump in the pattern, and d.  */
  4285.               if (mcnt < 0 && (re_opcode_t) *p1 == on_failure_jump
  4286.                   && (re_opcode_t) p1[3] == start_memory && p1[4] == *p)
  4287.         {
  4288.                   /* If this group ever matched anything, then restore
  4289.                      what its registers were before trying this last
  4290.                      failed match, e.g., with `(a*)*b' against `ab' for
  4291.                      regstart[1], and, e.g., with `((a*)*(b*)*)*'
  4292.                      against `aba' for regend[3].
  4293.  
  4294.                      Also restore the registers for inner groups for,
  4295.                      e.g., `((a*)(b*))*' against `aba' (register 3 would
  4296.                      otherwise get trashed).  */
  4297.  
  4298.                   if (EVER_MATCHED_SOMETHING (reg_info[*p]))
  4299.             {
  4300.               unsigned r;
  4301.  
  4302.                       EVER_MATCHED_SOMETHING (reg_info[*p]) = 0;
  4303.  
  4304.               /* Restore this and inner groups' (if any) registers.  */
  4305.                       for (r = *p; r < *p + *(p + 1); r++)
  4306.                         {
  4307.                           regstart[r] = old_regstart[r];
  4308.  
  4309.                           /* xx why this test?  */
  4310.                           if (old_regend[r] >= regstart[r])
  4311.                             regend[r] = old_regend[r];
  4312.                         }
  4313.                     }
  4314.           p1++;
  4315.                   EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  4316.                   PUSH_FAILURE_POINT (p1 + mcnt, d, -2);
  4317.  
  4318.                   goto fail;
  4319.                 }
  4320.             }
  4321.  
  4322.           /* Move past the register number and the inner group count.  */
  4323.           p += 2;
  4324.           break;
  4325.  
  4326.  
  4327.     /* \<digit> has been turned into a `duplicate' command which is
  4328.            followed by the numeric value of <digit> as the register number.  */
  4329.         case duplicate:
  4330.       {
  4331.         register const char *d2, *dend2;
  4332.         int regno = *p++;   /* Get which register to match against.  */
  4333.         DEBUG_PRINT2 ("EXECUTING duplicate %d.\n", regno);
  4334.  
  4335.         /* Can't back reference a group which we've never matched.  */
  4336.             if (REG_UNSET (regstart[regno]) || REG_UNSET (regend[regno]))
  4337.               goto fail;
  4338.  
  4339.             /* Where in input to try to start matching.  */
  4340.             d2 = regstart[regno];
  4341.  
  4342.             /* Where to stop matching; if both the place to start and
  4343.                the place to stop matching are in the same string, then
  4344.                set to the place to stop, otherwise, for now have to use
  4345.                the end of the first string.  */
  4346.  
  4347.             dend2 = ((FIRST_STRING_P (regstart[regno])
  4348.               == FIRST_STRING_P (regend[regno]))
  4349.              ? regend[regno] : end_match_1);
  4350.         for (;;)
  4351.           {
  4352.         /* If necessary, advance to next segment in register
  4353.                    contents.  */
  4354.         while (d2 == dend2)
  4355.           {
  4356.             if (dend2 == end_match_2) break;
  4357.             if (dend2 == regend[regno]) break;
  4358.  
  4359.                     /* End of string1 => advance to string2. */
  4360.                     d2 = string2;
  4361.                     dend2 = regend[regno];
  4362.           }
  4363.         /* At end of register contents => success */
  4364.         if (d2 == dend2) break;
  4365.  
  4366.         /* If necessary, advance to next segment in data.  */
  4367.         PREFETCH ();
  4368.  
  4369.         /* How many characters left in this segment to match.  */
  4370.         mcnt = dend - d;
  4371.  
  4372.         /* Want how many consecutive characters we can match in
  4373.                    one shot, so, if necessary, adjust the count.  */
  4374.                 if (mcnt > dend2 - d2)
  4375.           mcnt = dend2 - d2;
  4376.  
  4377.         /* Compare that many; failure if mismatch, else move
  4378.                    past them.  */
  4379.         if (translate
  4380.                     ? bcmp_translate (d, d2, mcnt, translate)
  4381.                     : bcmp (d, d2, mcnt))
  4382.           goto fail;
  4383.         d += mcnt, d2 += mcnt;
  4384.  
  4385.         /* Do this because we've match some characters.  */
  4386.         SET_REGS_MATCHED ();
  4387.           }
  4388.       }
  4389.       break;
  4390.  
  4391.  
  4392.         /* begline matches the empty string at the beginning of the string
  4393.            (unless `not_bol' is set in `bufp'), and, if
  4394.            `newline_anchor' is set, after newlines.  */
  4395.     case begline:
  4396.           DEBUG_PRINT1 ("EXECUTING begline.\n");
  4397.  
  4398.           if (AT_STRINGS_BEG (d))
  4399.             {
  4400.               if (!bufp->not_bol) break;
  4401.             }
  4402.           else if (d[-1] == '\n' && bufp->newline_anchor)
  4403.             {
  4404.               break;
  4405.             }
  4406.           /* In all other cases, we fail.  */
  4407.           goto fail;
  4408.  
  4409.  
  4410.         /* endline is the dual of begline.  */
  4411.     case endline:
  4412.           DEBUG_PRINT1 ("EXECUTING endline.\n");
  4413.  
  4414.           if (AT_STRINGS_END (d))
  4415.             {
  4416.               if (!bufp->not_eol) break;
  4417.             }
  4418.  
  4419.           /* We have to ``prefetch'' the next character.  */
  4420.           else if ((d == end1 ? *string2 : *d) == '\n'
  4421.                    && bufp->newline_anchor)
  4422.             {
  4423.               break;
  4424.             }
  4425.           goto fail;
  4426.  
  4427.  
  4428.     /* Match at the very beginning of the data.  */
  4429.         case begbuf:
  4430.           DEBUG_PRINT1 ("EXECUTING begbuf.\n");
  4431.           if (AT_STRINGS_BEG (d))
  4432.             break;
  4433.           goto fail;
  4434.  
  4435.  
  4436.     /* Match at the very end of the data.  */
  4437.         case endbuf:
  4438.           DEBUG_PRINT1 ("EXECUTING endbuf.\n");
  4439.       if (AT_STRINGS_END (d))
  4440.         break;
  4441.           goto fail;
  4442.  
  4443.  
  4444.         /* on_failure_keep_string_jump is used to optimize `.*\n'.  It
  4445.            pushes NULL as the value for the string on the stack.  Then
  4446.            `pop_failure_point' will keep the current value for the
  4447.            string, instead of restoring it.  To see why, consider
  4448.            matching `foo\nbar' against `.*\n'.  The .* matches the foo;
  4449.            then the . fails against the \n.  But the next thing we want
  4450.            to do is match the \n against the \n; if we restored the
  4451.            string value, we would be back at the foo.
  4452.  
  4453.            Because this is used only in specific cases, we don't need to
  4454.            check all the things that `on_failure_jump' does, to make
  4455.            sure the right things get saved on the stack.  Hence we don't
  4456.            share its code.  The only reason to push anything on the
  4457.            stack at all is that otherwise we would have to change
  4458.            `anychar's code to do something besides goto fail in this
  4459.            case; that seems worse than this.  */
  4460.         case on_failure_keep_string_jump:
  4461.           DEBUG_PRINT1 ("EXECUTING on_failure_keep_string_jump");
  4462.  
  4463.           EXTRACT_NUMBER_AND_INCR (mcnt, p);
  4464.           DEBUG_PRINT3 (" %d (to 0x%x):\n", mcnt, p + mcnt);
  4465.  
  4466.           PUSH_FAILURE_POINT (p + mcnt, NULL, -2);
  4467.           break;
  4468.  
  4469.  
  4470.     /* Uses of on_failure_jump:
  4471.  
  4472.            Each alternative starts with an on_failure_jump that points
  4473.            to the beginning of the next alternative.  Each alternative
  4474.            except the last ends with a jump that in effect jumps past
  4475.            the rest of the alternatives.  (They really jump to the
  4476.            ending jump of the following alternative, because tensioning
  4477.            these jumps is a hassle.)
  4478.  
  4479.            Repeats start with an on_failure_jump that points past both
  4480.            the repetition text and either the following jump or
  4481.            pop_failure_jump back to this on_failure_jump.  */
  4482.     case on_failure_jump:
  4483.         on_failure:
  4484.           DEBUG_PRINT1 ("EXECUTING on_failure_jump");
  4485.  
  4486.           EXTRACT_NUMBER_AND_INCR (mcnt, p);
  4487.           DEBUG_PRINT3 (" %d (to 0x%x)", mcnt, p + mcnt);
  4488.  
  4489.           /* If this on_failure_jump comes right before a group (i.e.,
  4490.              the original * applied to a group), save the information
  4491.              for that group and all inner ones, so that if we fail back
  4492.              to this point, the group's information will be correct.
  4493.              For example, in \(a*\)*\1, we need the preceding group,
  4494.              and in \(zz\(a*\)b*\)\2, we need the inner group.  */
  4495.  
  4496.           /* We can't use `p' to check ahead because we push
  4497.              a failure point to `p + mcnt' after we do this.  */
  4498.           p1 = p;
  4499.  
  4500.           /* We need to skip no_op's before we look for the
  4501.              start_memory in case this on_failure_jump is happening as
  4502.              the result of a completed succeed_n, as in \(a\)\{1,3\}b\1
  4503.              against aba.  */
  4504.           while (p1 < pend && (re_opcode_t) *p1 == no_op)
  4505.             p1++;
  4506.  
  4507.           if (p1 < pend && (re_opcode_t) *p1 == start_memory)
  4508.             {
  4509.               /* We have a new highest active register now.  This will
  4510.                  get reset at the start_memory we are about to get to,
  4511.                  but we will have saved all the registers relevant to
  4512.                  this repetition op, as described above.  */
  4513.               highest_active_reg = *(p1 + 1) + *(p1 + 2);
  4514.               if (lowest_active_reg == NO_LOWEST_ACTIVE_REG)
  4515.                 lowest_active_reg = *(p1 + 1);
  4516.             }
  4517.  
  4518.           DEBUG_PRINT1 (":\n");
  4519.           PUSH_FAILURE_POINT (p + mcnt, d, -2);
  4520.           break;
  4521.  
  4522.  
  4523.         /* A smart repeat ends with `maybe_pop_jump'.
  4524.        We change it to either `pop_failure_jump' or `jump'.  */
  4525.         case maybe_pop_jump:
  4526.           EXTRACT_NUMBER_AND_INCR (mcnt, p);
  4527.           DEBUG_PRINT2 ("EXECUTING maybe_pop_jump %d.\n", mcnt);
  4528.           {
  4529.         register unsigned char *p2 = p;
  4530.  
  4531.             /* Compare the beginning of the repeat with what in the
  4532.                pattern follows its end. If we can establish that there
  4533.                is nothing that they would both match, i.e., that we
  4534.                would have to backtrack because of (as in, e.g., `a*a')
  4535.                then we can change to pop_failure_jump, because we'll
  4536.                never have to backtrack.
  4537.  
  4538.                This is not true in the case of alternatives: in
  4539.                `(a|ab)*' we do need to backtrack to the `ab' alternative
  4540.                (e.g., if the string was `ab').  But instead of trying to
  4541.                detect that here, the alternative has put on a dummy
  4542.                failure point which is what we will end up popping.  */
  4543.  
  4544.         /* Skip over open/close-group commands.
  4545.            If what follows this loop is a ...+ construct,
  4546.            look at what begins its body, since we will have to
  4547.            match at least one of that.  */
  4548.         while (1)
  4549.           {
  4550.         if (p2 + 2 < pend
  4551.             && ((re_opcode_t) *p2 == stop_memory
  4552.             || (re_opcode_t) *p2 == start_memory))
  4553.           p2 += 3;
  4554.         else if (p2 + 6 < pend
  4555.              && (re_opcode_t) *p2 == dummy_failure_jump)
  4556.           p2 += 6;
  4557.         else
  4558.           break;
  4559.           }
  4560.  
  4561.         p1 = p + mcnt;
  4562.         /* p1[0] ... p1[2] are the `on_failure_jump' corresponding
  4563.            to the `maybe_finalize_jump' of this case.  Examine what
  4564.            follows.  */
  4565.  
  4566.             /* If we're at the end of the pattern, we can change.  */
  4567.             if (p2 == pend)
  4568.           {
  4569.         /* Consider what happens when matching ":\(.*\)"
  4570.            against ":/".  I don't really understand this code
  4571.            yet.  */
  4572.               p[-3] = (unsigned char) pop_failure_jump;
  4573.                 DEBUG_PRINT1
  4574.                   ("  End of pattern: change to `pop_failure_jump'.\n");
  4575.               }
  4576.  
  4577.             else if ((re_opcode_t) *p2 == exactn
  4578.              || (bufp->newline_anchor && (re_opcode_t) *p2 == endline))
  4579.           {
  4580.         register unsigned char c
  4581.                   = *p2 == (unsigned char) endline ? '\n' : p2[2];
  4582.  
  4583.                 if ((re_opcode_t) p1[3] == exactn && p1[5] != c)
  4584.                   {
  4585.               p[-3] = (unsigned char) pop_failure_jump;
  4586.                     DEBUG_PRINT3 ("  %c != %c => pop_failure_jump.\n",
  4587.                                   c, p1[5]);
  4588.                   }
  4589.  
  4590.         else if ((re_opcode_t) p1[3] == charset
  4591.              || (re_opcode_t) p1[3] == charset_not)
  4592.           {
  4593.             int not = (re_opcode_t) p1[3] == charset_not;
  4594.  
  4595.             if (c < (unsigned char) (p1[4] * BYTEWIDTH)
  4596.             && p1[5 + c / BYTEWIDTH] & (1 << (c % BYTEWIDTH)))
  4597.               not = !not;
  4598.  
  4599.                     /* `not' is equal to 1 if c would match, which means
  4600.                         that we can't change to pop_failure_jump.  */
  4601.             if (!not)
  4602.                       {
  4603.                   p[-3] = (unsigned char) pop_failure_jump;
  4604.                         DEBUG_PRINT1 ("  No match => pop_failure_jump.\n");
  4605.                       }
  4606.           }
  4607.           }
  4608.             else if ((re_opcode_t) *p2 == charset)
  4609.           {
  4610. #ifdef DEBUG
  4611.         register unsigned char c
  4612.                   = *p2 == (unsigned char) endline ? '\n' : p2[2];
  4613. #endif
  4614.  
  4615.                 if ((re_opcode_t) p1[3] == exactn
  4616.             && ! ((int) p2[1] * BYTEWIDTH > (int) p1[4]
  4617.               && (p2[1 + p1[4] / BYTEWIDTH]
  4618.                   & (1 << (p1[4] % BYTEWIDTH)))))
  4619.                   {
  4620.               p[-3] = (unsigned char) pop_failure_jump;
  4621.                     DEBUG_PRINT3 ("  %c != %c => pop_failure_jump.\n",
  4622.                                   c, p1[5]);
  4623.                   }
  4624.  
  4625.         else if ((re_opcode_t) p1[3] == charset_not)
  4626.           {
  4627.             int idx;
  4628.             /* We win if the charset_not inside the loop
  4629.                lists every character listed in the charset after.  */
  4630.             for (idx = 0; idx < (int) p2[1]; idx++)
  4631.               if (! (p2[2 + idx] == 0
  4632.                  || (idx < (int) p1[4]
  4633.                  && ((p2[2 + idx] & ~ p1[5 + idx]) == 0))))
  4634.             break;
  4635.  
  4636.             if (idx == p2[1])
  4637.                       {
  4638.                   p[-3] = (unsigned char) pop_failure_jump;
  4639.                         DEBUG_PRINT1 ("  No match => pop_failure_jump.\n");
  4640.                       }
  4641.           }
  4642.         else if ((re_opcode_t) p1[3] == charset)
  4643.           {
  4644.             int idx;
  4645.             /* We win if the charset inside the loop
  4646.                has no overlap with the one after the loop.  */
  4647.             for (idx = 0;
  4648.              idx < (int) p2[1] && idx < (int) p1[4];
  4649.              idx++)
  4650.               if ((p2[2 + idx] & p1[5 + idx]) != 0)
  4651.             break;
  4652.  
  4653.             if (idx == p2[1] || idx == p1[4])
  4654.                       {
  4655.                   p[-3] = (unsigned char) pop_failure_jump;
  4656.                         DEBUG_PRINT1 ("  No match => pop_failure_jump.\n");
  4657.                       }
  4658.           }
  4659.           }
  4660.       }
  4661.       p -= 2;        /* Point at relative address again.  */
  4662.       if ((re_opcode_t) p[-1] != pop_failure_jump)
  4663.         {
  4664.           p[-1] = (unsigned char) jump;
  4665.               DEBUG_PRINT1 ("  Match => jump.\n");
  4666.           goto unconditional_jump;
  4667.         }
  4668.         /* Note fall through.  */
  4669.  
  4670.  
  4671.     /* The end of a simple repeat has a pop_failure_jump back to
  4672.            its matching on_failure_jump, where the latter will push a
  4673.            failure point.  The pop_failure_jump takes off failure
  4674.            points put on by this pop_failure_jump's matching
  4675.            on_failure_jump; we got through the pattern to here from the
  4676.            matching on_failure_jump, so didn't fail.  */
  4677.         case pop_failure_jump:
  4678.           {
  4679.             /* We need to pass separate storage for the lowest and
  4680.                highest registers, even though we don't care about the
  4681.                actual values.  Otherwise, we will restore only one
  4682.                register from the stack, since lowest will == highest in
  4683.                `pop_failure_point'.  */
  4684.             active_reg_t dummy_low_reg, dummy_high_reg;
  4685.             unsigned char *pdummy;
  4686.             const char *sdummy;
  4687.  
  4688.             DEBUG_PRINT1 ("EXECUTING pop_failure_jump.\n");
  4689.             POP_FAILURE_POINT (sdummy, pdummy,
  4690.                                dummy_low_reg, dummy_high_reg,
  4691.                                reg_dummy, reg_dummy, reg_info_dummy);
  4692.           }
  4693.           /* Note fall through.  */
  4694.  
  4695.  
  4696.         /* Unconditionally jump (without popping any failure points).  */
  4697.         case jump:
  4698.     unconditional_jump:
  4699.       EXTRACT_NUMBER_AND_INCR (mcnt, p);    /* Get the amount to jump.  */
  4700.           DEBUG_PRINT2 ("EXECUTING jump %d ", mcnt);
  4701.       p += mcnt;                /* Do the jump.  */
  4702.           DEBUG_PRINT2 ("(to 0x%x).\n", p);
  4703.       break;
  4704.  
  4705.  
  4706.         /* We need this opcode so we can detect where alternatives end
  4707.            in `group_match_null_string_p' et al.  */
  4708.         case jump_past_alt:
  4709.           DEBUG_PRINT1 ("EXECUTING jump_past_alt.\n");
  4710.           goto unconditional_jump;
  4711.  
  4712.  
  4713.         /* Normally, the on_failure_jump pushes a failure point, which
  4714.            then gets popped at pop_failure_jump.  We will end up at
  4715.            pop_failure_jump, also, and with a pattern of, say, `a+', we
  4716.            are skipping over the on_failure_jump, so we have to push
  4717.            something meaningless for pop_failure_jump to pop.  */
  4718.         case dummy_failure_jump:
  4719.           DEBUG_PRINT1 ("EXECUTING dummy_failure_jump.\n");
  4720.           /* It doesn't matter what we push for the string here.  What
  4721.              the code at `fail' tests is the value for the pattern.  */
  4722.           PUSH_FAILURE_POINT (0, 0, -2);
  4723.           goto unconditional_jump;
  4724.  
  4725.  
  4726.         /* At the end of an alternative, we need to push a dummy failure
  4727.            point in case we are followed by a `pop_failure_jump', because
  4728.            we don't want the failure point for the alternative to be
  4729.            popped.  For example, matching `(a|ab)*' against `aab'
  4730.            requires that we match the `ab' alternative.  */
  4731.         case push_dummy_failure:
  4732.           DEBUG_PRINT1 ("EXECUTING push_dummy_failure.\n");
  4733.           /* See comments just above at `dummy_failure_jump' about the
  4734.              two zeroes.  */
  4735.           PUSH_FAILURE_POINT (0, 0, -2);
  4736.           break;
  4737.  
  4738.         /* Have to succeed matching what follows at least n times.
  4739.            After that, handle like `on_failure_jump'.  */
  4740.         case succeed_n:
  4741.           EXTRACT_NUMBER (mcnt, p + 2);
  4742.           DEBUG_PRINT2 ("EXECUTING succeed_n %d.\n", mcnt);
  4743.  
  4744.           assert (mcnt >= 0);
  4745.           /* Originally, this is how many times we HAVE to succeed.  */
  4746.           if (mcnt > 0)
  4747.             {
  4748.                mcnt--;
  4749.            p += 2;
  4750.                STORE_NUMBER_AND_INCR (p, mcnt);
  4751.                DEBUG_PRINT3 ("  Setting 0x%x to %d.\n", p, mcnt);
  4752.             }
  4753.       else if (mcnt == 0)
  4754.             {
  4755.               DEBUG_PRINT2 ("  Setting two bytes from 0x%x to no_op.\n", p+2);
  4756.           p[2] = (unsigned char) no_op;
  4757.               p[3] = (unsigned char) no_op;
  4758.               goto on_failure;
  4759.             }
  4760.           break;
  4761.  
  4762.         case jump_n:
  4763.           EXTRACT_NUMBER (mcnt, p + 2);
  4764.           DEBUG_PRINT2 ("EXECUTING jump_n %d.\n", mcnt);
  4765.  
  4766.           /* Originally, this is how many times we CAN jump.  */
  4767.           if (mcnt)
  4768.             {
  4769.                mcnt--;
  4770.                STORE_NUMBER (p + 2, mcnt);
  4771.            goto unconditional_jump;
  4772.             }
  4773.           /* If don't have to jump any more, skip over the rest of command.  */
  4774.       else
  4775.         p += 4;
  4776.           break;
  4777.  
  4778.     case set_number_at:
  4779.       {
  4780.             DEBUG_PRINT1 ("EXECUTING set_number_at.\n");
  4781.  
  4782.             EXTRACT_NUMBER_AND_INCR (mcnt, p);
  4783.             p1 = p + mcnt;
  4784.             EXTRACT_NUMBER_AND_INCR (mcnt, p);
  4785.             DEBUG_PRINT3 ("  Setting 0x%x to %d.\n", p1, mcnt);
  4786.         STORE_NUMBER (p1, mcnt);
  4787.             break;
  4788.           }
  4789.  
  4790.         case wordbound:
  4791.           DEBUG_PRINT1 ("EXECUTING wordbound.\n");
  4792.           if (AT_WORD_BOUNDARY (d))
  4793.         break;
  4794.           goto fail;
  4795.  
  4796.     case notwordbound:
  4797.           DEBUG_PRINT1 ("EXECUTING notwordbound.\n");
  4798.       if (AT_WORD_BOUNDARY (d))
  4799.         goto fail;
  4800.           break;
  4801.  
  4802.     case wordbeg:
  4803.           DEBUG_PRINT1 ("EXECUTING wordbeg.\n");
  4804.       if (WORDCHAR_P (d) && (AT_STRINGS_BEG (d) || !WORDCHAR_P (d - 1)))
  4805.         break;
  4806.           goto fail;
  4807.  
  4808.     case wordend:
  4809.           DEBUG_PRINT1 ("EXECUTING wordend.\n");
  4810.       if (!AT_STRINGS_BEG (d) && WORDCHAR_P (d - 1)
  4811.               && (!WORDCHAR_P (d) || AT_STRINGS_END (d)))
  4812.         break;
  4813.           goto fail;
  4814.  
  4815. #ifdef emacs
  4816.       case before_dot:
  4817.           DEBUG_PRINT1 ("EXECUTING before_dot.\n");
  4818.        if (PTR_CHAR_POS ((unsigned char *) d) >= point)
  4819.           goto fail;
  4820.         break;
  4821.  
  4822.       case at_dot:
  4823.           DEBUG_PRINT1 ("EXECUTING at_dot.\n");
  4824.        if (PTR_CHAR_POS ((unsigned char *) d) != point)
  4825.           goto fail;
  4826.         break;
  4827.  
  4828.       case after_dot:
  4829.           DEBUG_PRINT1 ("EXECUTING after_dot.\n");
  4830.           if (PTR_CHAR_POS ((unsigned char *) d) <= point)
  4831.           goto fail;
  4832.         break;
  4833.  
  4834.     case syntaxspec:
  4835.           DEBUG_PRINT2 ("EXECUTING syntaxspec %d.\n", mcnt);
  4836.       mcnt = *p++;
  4837.       goto matchsyntax;
  4838.  
  4839.         case wordchar:
  4840.           DEBUG_PRINT1 ("EXECUTING Emacs wordchar.\n");
  4841.       mcnt = (int) Sword;
  4842.         matchsyntax:
  4843.       PREFETCH ();
  4844.       /* Can't use *d++ here; SYNTAX may be an unsafe macro.  */
  4845.       d++;
  4846.       if (SYNTAX (d[-1]) != (enum syntaxcode) mcnt)
  4847.         goto fail;
  4848.           SET_REGS_MATCHED ();
  4849.       break;
  4850.  
  4851.     case notsyntaxspec:
  4852.           DEBUG_PRINT2 ("EXECUTING notsyntaxspec %d.\n", mcnt);
  4853.       mcnt = *p++;
  4854.       goto matchnotsyntax;
  4855.  
  4856.         case notwordchar:
  4857.           DEBUG_PRINT1 ("EXECUTING Emacs notwordchar.\n");
  4858.       mcnt = (int) Sword;
  4859.         matchnotsyntax:
  4860.       PREFETCH ();
  4861.       /* Can't use *d++ here; SYNTAX may be an unsafe macro.  */
  4862.       d++;
  4863.       if (SYNTAX (d[-1]) == (enum syntaxcode) mcnt)
  4864.         goto fail;
  4865.       SET_REGS_MATCHED ();
  4866.           break;
  4867.  
  4868. #else /* not emacs */
  4869.     case wordchar:
  4870.           DEBUG_PRINT1 ("EXECUTING non-Emacs wordchar.\n");
  4871.       PREFETCH ();
  4872.           if (!WORDCHAR_P (d))
  4873.             goto fail;
  4874.       SET_REGS_MATCHED ();
  4875.           d++;
  4876.       break;
  4877.  
  4878.     case notwordchar:
  4879.           DEBUG_PRINT1 ("EXECUTING non-Emacs notwordchar.\n");
  4880.       PREFETCH ();
  4881.       if (WORDCHAR_P (d))
  4882.             goto fail;
  4883.           SET_REGS_MATCHED ();
  4884.           d++;
  4885.       break;
  4886. #endif /* not emacs */
  4887.  
  4888.         default:
  4889.           abort ();
  4890.     }
  4891.       continue;  /* Successfully executed one pattern command; keep going.  */
  4892.  
  4893.  
  4894.     /* We goto here if a matching operation fails. */
  4895.     fail:
  4896.       if (!FAIL_STACK_EMPTY ())
  4897.     { /* A restart point is known.  Restore to that state.  */
  4898.           DEBUG_PRINT1 ("\nFAIL:\n");
  4899.           POP_FAILURE_POINT (d, p,
  4900.                              lowest_active_reg, highest_active_reg,
  4901.                              regstart, regend, reg_info);
  4902.  
  4903.           /* If this failure point is a dummy, try the next one.  */
  4904.           if (!p)
  4905.         goto fail;
  4906.  
  4907.           /* If we failed to the end of the pattern, don't examine *p.  */
  4908.       assert (p <= pend);
  4909.           if (p < pend)
  4910.             {
  4911.               boolean is_a_jump_n = false;
  4912.  
  4913.               /* If failed to a backwards jump that's part of a repetition
  4914.                  loop, need to pop this failure point and use the next one.  */
  4915.               switch ((re_opcode_t) *p)
  4916.                 {
  4917.                 case jump_n:
  4918.                   is_a_jump_n = true;
  4919.                 case maybe_pop_jump:
  4920.                 case pop_failure_jump:
  4921.                 case jump:
  4922.                   p1 = p + 1;
  4923.                   EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  4924.                   p1 += mcnt;
  4925.  
  4926.                   if ((is_a_jump_n && (re_opcode_t) *p1 == succeed_n)
  4927.                       || (!is_a_jump_n
  4928.                           && (re_opcode_t) *p1 == on_failure_jump))
  4929.                     goto fail;
  4930.                   break;
  4931.                 default:
  4932.                   /* do nothing */ ;
  4933.                 }
  4934.             }
  4935.  
  4936.           if (d >= string1 && d <= end1)
  4937.         dend = end_match_1;
  4938.         }
  4939.       else
  4940.         break;   /* Matching at this starting point really fails.  */
  4941.     } /* for (;;) */
  4942.  
  4943.   if (best_regs_set)
  4944.     goto restore_best_regs;
  4945.  
  4946.   FREE_VARIABLES ();
  4947.  
  4948.   return -1;                     /* Failure to match.  */
  4949. } /* re_match_2 */
  4950.  
  4951. /* Subroutine definitions for re_match_2.  */
  4952.  
  4953.  
  4954. /* We are passed P pointing to a register number after a start_memory.
  4955.  
  4956.    Return true if the pattern up to the corresponding stop_memory can
  4957.    match the empty string, and false otherwise.
  4958.  
  4959.    If we find the matching stop_memory, sets P to point to one past its number.
  4960.    Otherwise, sets P to an undefined byte less than or equal to END.
  4961.  
  4962.    We don't handle duplicates properly (yet).  */
  4963.  
  4964. static boolean
  4965. group_match_null_string_p (p, end, reg_info)
  4966.     unsigned char **p, *end;
  4967.     register_info_type *reg_info;
  4968. {
  4969.   int mcnt;
  4970.   /* Point to after the args to the start_memory.  */
  4971.   unsigned char *p1 = *p + 2;
  4972.  
  4973.   while (p1 < end)
  4974.     {
  4975.       /* Skip over opcodes that can match nothing, and return true or
  4976.      false, as appropriate, when we get to one that can't, or to the
  4977.          matching stop_memory.  */
  4978.  
  4979.       switch ((re_opcode_t) *p1)
  4980.         {
  4981.         /* Could be either a loop or a series of alternatives.  */
  4982.         case on_failure_jump:
  4983.           p1++;
  4984.           EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  4985.  
  4986.           /* If the next operation is not a jump backwards in the
  4987.          pattern.  */
  4988.  
  4989.       if (mcnt >= 0)
  4990.         {
  4991.               /* Go through the on_failure_jumps of the alternatives,
  4992.                  seeing if any of the alternatives cannot match nothing.
  4993.                  The last alternative starts with only a jump,
  4994.                  whereas the rest start with on_failure_jump and end
  4995.                  with a jump, e.g., here is the pattern for `a|b|c':
  4996.  
  4997.                  /on_failure_jump/0/6/exactn/1/a/jump_past_alt/0/6
  4998.                  /on_failure_jump/0/6/exactn/1/b/jump_past_alt/0/3
  4999.                  /exactn/1/c
  5000.  
  5001.                  So, we have to first go through the first (n-1)
  5002.                  alternatives and then deal with the last one separately.  */
  5003.  
  5004.  
  5005.               /* Deal with the first (n-1) alternatives, which start
  5006.                  with an on_failure_jump (see above) that jumps to right
  5007.                  past a jump_past_alt.  */
  5008.  
  5009.               while ((re_opcode_t) p1[mcnt-3] == jump_past_alt)
  5010.                 {
  5011.                   /* `mcnt' holds how many bytes long the alternative
  5012.                      is, including the ending `jump_past_alt' and
  5013.                      its number.  */
  5014.  
  5015.                   if (!alt_match_null_string_p (p1, p1 + mcnt - 3,
  5016.                                       reg_info))
  5017.                     return false;
  5018.  
  5019.                   /* Move to right after this alternative, including the
  5020.              jump_past_alt.  */
  5021.                   p1 += mcnt;
  5022.  
  5023.                   /* Break if it's the beginning of an n-th alternative
  5024.                      that doesn't begin with an on_failure_jump.  */
  5025.                   if ((re_opcode_t) *p1 != on_failure_jump)
  5026.                     break;
  5027.  
  5028.           /* Still have to check that it's not an n-th
  5029.              alternative that starts with an on_failure_jump.  */
  5030.           p1++;
  5031.                   EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  5032.                   if ((re_opcode_t) p1[mcnt-3] != jump_past_alt)
  5033.                     {
  5034.               /* Get to the beginning of the n-th alternative.  */
  5035.                       p1 -= 3;
  5036.                       break;
  5037.                     }
  5038.                 }
  5039.  
  5040.               /* Deal with the last alternative: go back and get number
  5041.                  of the `jump_past_alt' just before it.  `mcnt' contains
  5042.                  the length of the alternative.  */
  5043.               EXTRACT_NUMBER (mcnt, p1 - 2);
  5044.  
  5045.               if (!alt_match_null_string_p (p1, p1 + mcnt, reg_info))
  5046.                 return false;
  5047.  
  5048.               p1 += mcnt;    /* Get past the n-th alternative.  */
  5049.             } /* if mcnt > 0 */
  5050.           break;
  5051.  
  5052.  
  5053.         case stop_memory:
  5054.       assert (p1[1] == **p);
  5055.           *p = p1 + 2;
  5056.           return true;
  5057.  
  5058.  
  5059.         default:
  5060.           if (!common_op_match_null_string_p (&p1, end, reg_info))
  5061.             return false;
  5062.         }
  5063.     } /* while p1 < end */
  5064.  
  5065.   return false;
  5066. } /* group_match_null_string_p */
  5067.  
  5068.  
  5069. /* Similar to group_match_null_string_p, but doesn't deal with alternatives:
  5070.    It expects P to be the first byte of a single alternative and END one
  5071.    byte past the last. The alternative can contain groups.  */
  5072.  
  5073. static boolean
  5074. alt_match_null_string_p (p, end, reg_info)
  5075.     unsigned char *p, *end;
  5076.     register_info_type *reg_info;
  5077. {
  5078.   int mcnt;
  5079.   unsigned char *p1 = p;
  5080.  
  5081.   while (p1 < end)
  5082.     {
  5083.       /* Skip over opcodes that can match nothing, and break when we get
  5084.          to one that can't.  */
  5085.  
  5086.       switch ((re_opcode_t) *p1)
  5087.         {
  5088.     /* It's a loop.  */
  5089.         case on_failure_jump:
  5090.           p1++;
  5091.           EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  5092.           p1 += mcnt;
  5093.           break;
  5094.  
  5095.     default:
  5096.           if (!common_op_match_null_string_p (&p1, end, reg_info))
  5097.             return false;
  5098.         }
  5099.     }  /* while p1 < end */
  5100.  
  5101.   return true;
  5102. } /* alt_match_null_string_p */
  5103.  
  5104.  
  5105. /* Deals with the ops common to group_match_null_string_p and
  5106.    alt_match_null_string_p.
  5107.  
  5108.    Sets P to one after the op and its arguments, if any.  */
  5109.  
  5110. static boolean
  5111. common_op_match_null_string_p (p, end, reg_info)
  5112.     unsigned char **p, *end;
  5113.     register_info_type *reg_info;
  5114. {
  5115.   int mcnt;
  5116.   boolean ret;
  5117.   int reg_no;
  5118.   unsigned char *p1 = *p;
  5119.  
  5120.   switch ((re_opcode_t) *p1++)
  5121.     {
  5122.     case no_op:
  5123.     case begline:
  5124.     case endline:
  5125.     case begbuf:
  5126.     case endbuf:
  5127.     case wordbeg:
  5128.     case wordend:
  5129.     case wordbound:
  5130.     case notwordbound:
  5131. #ifdef emacs
  5132.     case before_dot:
  5133.     case at_dot:
  5134.     case after_dot:
  5135. #endif
  5136.       break;
  5137.  
  5138.     case start_memory:
  5139.       reg_no = *p1;
  5140.       assert (reg_no > 0 && reg_no <= MAX_REGNUM);
  5141.       ret = group_match_null_string_p (&p1, end, reg_info);
  5142.  
  5143.       /* Have to set this here in case we're checking a group which
  5144.          contains a group and a back reference to it.  */
  5145.  
  5146.       if (REG_MATCH_NULL_STRING_P (reg_info[reg_no]) == MATCH_NULL_UNSET_VALUE)
  5147.         REG_MATCH_NULL_STRING_P (reg_info[reg_no]) = ret;
  5148.  
  5149.       if (!ret)
  5150.         return false;
  5151.       break;
  5152.  
  5153.     /* If this is an optimized succeed_n for zero times, make the jump.  */
  5154.     case jump:
  5155.       EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  5156.       if (mcnt >= 0)
  5157.         p1 += mcnt;
  5158.       else
  5159.         return false;
  5160.       break;
  5161.  
  5162.     case succeed_n:
  5163.       /* Get to the number of times to succeed.  */
  5164.       p1 += 2;
  5165.       EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  5166.  
  5167.       if (mcnt == 0)
  5168.         {
  5169.           p1 -= 4;
  5170.           EXTRACT_NUMBER_AND_INCR (mcnt, p1);
  5171.           p1 += mcnt;
  5172.         }
  5173.       else
  5174.         return false;
  5175.       break;
  5176.  
  5177.     case duplicate:
  5178.       if (!REG_MATCH_NULL_STRING_P (reg_info[*p1]))
  5179.         return false;
  5180.       break;
  5181.  
  5182.     case set_number_at:
  5183.       p1 += 4;
  5184.  
  5185.     default:
  5186.       /* All other opcodes mean we cannot match the empty string.  */
  5187.       return false;
  5188.   }
  5189.  
  5190.   *p = p1;
  5191.   return true;
  5192. } /* common_op_match_null_string_p */
  5193.  
  5194.  
  5195. /* Return zero if TRANSLATE[S1] and TRANSLATE[S2] are identical for LEN
  5196.    bytes; nonzero otherwise.  */
  5197.  
  5198. static int
  5199. bcmp_translate (s1, s2, len, translate)
  5200.      const char *s1, *s2;
  5201.      register int len;
  5202.      RE_TRANSLATE_TYPE translate;
  5203. {
  5204.   register const unsigned char *p1 = (const unsigned char *) s1,
  5205.                    *p2 = (const unsigned char *) s2;
  5206.   while (len)
  5207.     {
  5208.       if (translate[*p1++] != translate[*p2++]) return 1;
  5209.       len--;
  5210.     }
  5211.   return 0;
  5212. }
  5213.  
  5214. /* Entry points for GNU code.  */
  5215.  
  5216. /* re_compile_pattern is the GNU regular expression compiler: it
  5217.    compiles PATTERN (of length SIZE) and puts the result in BUFP.
  5218.    Returns 0 if the pattern was valid, otherwise an error string.
  5219.  
  5220.    Assumes the `allocated' (and perhaps `buffer') and `translate' fields
  5221.    are set in BUFP on entry.
  5222.  
  5223.    We call regex_compile to do the actual compilation.  */
  5224.  
  5225. const char *
  5226. re_compile_pattern (pattern, length, bufp)
  5227.      const char *pattern;
  5228.      size_t length;
  5229.      struct re_pattern_buffer *bufp;
  5230. {
  5231.   reg_errcode_t ret;
  5232.  
  5233.   /* GNU code is written to assume at least RE_NREGS registers will be set
  5234.      (and at least one extra will be -1).  */
  5235.   bufp->regs_allocated = REGS_UNALLOCATED;
  5236.  
  5237.   /* And GNU code determines whether or not to get register information
  5238.      by passing null for the REGS argument to re_match, etc., not by
  5239.      setting no_sub.  */
  5240.   bufp->no_sub = 0;
  5241.  
  5242.   /* Match anchors at newline.  */
  5243.   bufp->newline_anchor = 1;
  5244.  
  5245.   ret = regex_compile (pattern, length, re_syntax_options, bufp);
  5246.  
  5247.   if (!ret)
  5248.     return NULL;
  5249.   return gettext (re_error_msgid[(int) ret]);
  5250. }
  5251.  
  5252. /* Entry points compatible with 4.2 BSD regex library.  We don't define
  5253.    them unless specifically requested.  */
  5254.  
  5255. #ifdef _REGEX_RE_COMP
  5256.  
  5257. /* BSD has one and only one pattern buffer.  */
  5258. static struct re_pattern_buffer re_comp_buf;
  5259.  
  5260. char *
  5261. re_comp (s)
  5262.     const char *s;
  5263. {
  5264.   reg_errcode_t ret;
  5265.  
  5266.   if (!s)
  5267.     {
  5268.       if (!re_comp_buf.buffer)
  5269.     return gettext ("No previous regular expression");
  5270.       return 0;
  5271.     }
  5272.  
  5273.   if (!re_comp_buf.buffer)
  5274.     {
  5275.       re_comp_buf.buffer = (unsigned char *) malloc (200);
  5276.       if (re_comp_buf.buffer == NULL)
  5277.         return gettext (re_error_msgid[(int) REG_ESPACE]);
  5278.       re_comp_buf.allocated = 200;
  5279.  
  5280.       re_comp_buf.fastmap = (char *) malloc (1 << BYTEWIDTH);
  5281.       if (re_comp_buf.fastmap == NULL)
  5282.     return gettext (re_error_msgid[(int) REG_ESPACE]);
  5283.     }
  5284.  
  5285.   /* Since `re_exec' always passes NULL for the `regs' argument, we
  5286.      don't need to initialize the pattern buffer fields which affect it.  */
  5287.  
  5288.   /* Match anchors at newlines.  */
  5289.   re_comp_buf.newline_anchor = 1;
  5290.  
  5291.   ret = regex_compile (s, strlen (s), re_syntax_options, &re_comp_buf);
  5292.  
  5293.   if (!ret)
  5294.     return NULL;
  5295.  
  5296.   /* Yes, we're discarding `const' here if !HAVE_LIBINTL.  */
  5297.   return (char *) gettext (re_error_msgid[(int) ret]);
  5298. }
  5299.  
  5300.  
  5301. int
  5302. re_exec (s)
  5303.     const char *s;
  5304. {
  5305.   const int len = strlen (s);
  5306.   return
  5307.     0 <= re_search (&re_comp_buf, s, len, 0, len, (struct re_registers *) 0);
  5308. }
  5309. #endif /* _REGEX_RE_COMP */
  5310.  
  5311. /* POSIX.2 functions.  Don't define these for Emacs.  */
  5312.  
  5313. #ifndef emacs
  5314.  
  5315. /* regcomp takes a regular expression as a string and compiles it.
  5316.  
  5317.    PREG is a regex_t *.  We do not expect any fields to be initialized,
  5318.    since POSIX says we shouldn't.  Thus, we set
  5319.  
  5320.      `buffer' to the compiled pattern;
  5321.      `used' to the length of the compiled pattern;
  5322.      `syntax' to RE_SYNTAX_POSIX_EXTENDED if the
  5323.        REG_EXTENDED bit in CFLAGS is set; otherwise, to
  5324.        RE_SYNTAX_POSIX_BASIC;
  5325.      `newline_anchor' to REG_NEWLINE being set in CFLAGS;
  5326.      `fastmap' and `fastmap_accurate' to zero;
  5327.      `re_nsub' to the number of subexpressions in PATTERN.
  5328.  
  5329.    PATTERN is the address of the pattern string.
  5330.  
  5331.    CFLAGS is a series of bits which affect compilation.
  5332.  
  5333.      If REG_EXTENDED is set, we use POSIX extended syntax; otherwise, we
  5334.      use POSIX basic syntax.
  5335.  
  5336.      If REG_NEWLINE is set, then . and [^...] don't match newline.
  5337.      Also, regexec will try a match beginning after every newline.
  5338.  
  5339.      If REG_ICASE is set, then we considers upper- and lowercase
  5340.      versions of letters to be equivalent when matching.
  5341.  
  5342.      If REG_NOSUB is set, then when PREG is passed to regexec, that
  5343.      routine will report only success or failure, and nothing about the
  5344.      registers.
  5345.  
  5346.    It returns 0 if it succeeds, nonzero if it doesn't.  (See regex.h for
  5347.    the return codes and their meanings.)  */
  5348.  
  5349. int
  5350. regcomp (preg, pattern, cflags)
  5351.     regex_t *preg;
  5352.     const char *pattern;
  5353.     int cflags;
  5354. {
  5355.   reg_errcode_t ret;
  5356.   reg_syntax_t syntax
  5357.     = (cflags & REG_EXTENDED) ?
  5358.       RE_SYNTAX_POSIX_EXTENDED : RE_SYNTAX_POSIX_BASIC;
  5359.  
  5360.   /* regex_compile will allocate the space for the compiled pattern.  */
  5361.   preg->buffer = 0;
  5362.   preg->allocated = 0;
  5363.   preg->used = 0;
  5364.  
  5365.   /* Don't bother to use a fastmap when searching.  This simplifies the
  5366.      REG_NEWLINE case: if we used a fastmap, we'd have to put all the
  5367.      characters after newlines into the fastmap.  This way, we just try
  5368.      every character.  */
  5369.   preg->fastmap = 0;
  5370.  
  5371.   if (cflags & REG_ICASE)
  5372.     {
  5373.       unsigned i;
  5374.  
  5375.       preg->translate
  5376.     = (RE_TRANSLATE_TYPE) malloc (CHAR_SET_SIZE
  5377.                       * sizeof (*(RE_TRANSLATE_TYPE)0));
  5378.       if (preg->translate == NULL)
  5379.         return (int) REG_ESPACE;
  5380.  
  5381.       /* Map uppercase characters to corresponding lowercase ones.  */
  5382.       for (i = 0; i < CHAR_SET_SIZE; i++)
  5383.         preg->translate[i] = ISUPPER (i) ? tolower (i) : i;
  5384.     }
  5385.   else
  5386.     preg->translate = NULL;
  5387.  
  5388.   /* If REG_NEWLINE is set, newlines are treated differently.  */
  5389.   if (cflags & REG_NEWLINE)
  5390.     { /* REG_NEWLINE implies neither . nor [^...] match newline.  */
  5391.       syntax &= ~RE_DOT_NEWLINE;
  5392.       syntax |= RE_HAT_LISTS_NOT_NEWLINE;
  5393.       /* It also changes the matching behavior.  */
  5394.       preg->newline_anchor = 1;
  5395.     }
  5396.   else
  5397.     preg->newline_anchor = 0;
  5398.  
  5399.   preg->no_sub = !!(cflags & REG_NOSUB);
  5400.  
  5401.   /* POSIX says a null character in the pattern terminates it, so we
  5402.      can use strlen here in compiling the pattern.  */
  5403.   ret = regex_compile (pattern, strlen (pattern), syntax, preg);
  5404.  
  5405.   /* POSIX doesn't distinguish between an unmatched open-group and an
  5406.      unmatched close-group: both are REG_EPAREN.  */
  5407.   if (ret == REG_ERPAREN) ret = REG_EPAREN;
  5408.  
  5409.   return (int) ret;
  5410. }
  5411.  
  5412.  
  5413. /* regexec searches for a given pattern, specified by PREG, in the
  5414.    string STRING.
  5415.  
  5416.    If NMATCH is zero or REG_NOSUB was set in the cflags argument to
  5417.    `regcomp', we ignore PMATCH.  Otherwise, we assume PMATCH has at
  5418.    least NMATCH elements, and we set them to the offsets of the
  5419.    corresponding matched substrings.
  5420.  
  5421.    EFLAGS specifies `execution flags' which affect matching: if
  5422.    REG_NOTBOL is set, then ^ does not match at the beginning of the
  5423.    string; if REG_NOTEOL is set, then $ does not match at the end.
  5424.  
  5425.    We return 0 if we find a match and REG_NOMATCH if not.  */
  5426.  
  5427. int
  5428. regexec (preg, string, nmatch, pmatch, eflags)
  5429.     const regex_t *preg;
  5430.     const char *string;
  5431.     size_t nmatch;
  5432.     regmatch_t pmatch[];
  5433.     int eflags;
  5434. {
  5435.   int ret;
  5436.   struct re_registers regs;
  5437.   regex_t private_preg;
  5438.   int len = strlen (string);
  5439.   boolean want_reg_info = !preg->no_sub && nmatch > 0;
  5440.  
  5441.   private_preg = *preg;
  5442.  
  5443.   private_preg.not_bol = !!(eflags & REG_NOTBOL);
  5444.   private_preg.not_eol = !!(eflags & REG_NOTEOL);
  5445.  
  5446.   /* The user has told us exactly how many registers to return
  5447.      information about, via `nmatch'.  We have to pass that on to the
  5448.      matching routines.  */
  5449.   private_preg.regs_allocated = REGS_FIXED;
  5450.  
  5451.   if (want_reg_info)
  5452.     {
  5453.       regs.num_regs = nmatch;
  5454.       regs.start = TALLOC (nmatch, regoff_t);
  5455.       regs.end = TALLOC (nmatch, regoff_t);
  5456.       if (regs.start == NULL || regs.end == NULL)
  5457.         return (int) REG_NOMATCH;
  5458.     }
  5459.  
  5460.   /* Perform the searching operation.  */
  5461.   ret = re_search (&private_preg, string, len,
  5462.                    /* start: */ 0, /* range: */ len,
  5463.                    want_reg_info ? ®s : (struct re_registers *) 0);
  5464.  
  5465.   /* Copy the register information to the POSIX structure.  */
  5466.   if (want_reg_info)
  5467.     {
  5468.       if (ret >= 0)
  5469.         {
  5470.           unsigned r;
  5471.  
  5472.           for (r = 0; r < nmatch; r++)
  5473.             {
  5474.               pmatch[r].rm_so = regs.start[r];
  5475.               pmatch[r].rm_eo = regs.end[r];
  5476.             }
  5477.         }
  5478.  
  5479.       /* If we needed the temporary register info, free the space now.  */
  5480.       free (regs.start);
  5481.       free (regs.end);
  5482.     }
  5483.  
  5484.   /* We want zero return to mean success, unlike `re_search'.  */
  5485.   return ret >= 0 ? (int) REG_NOERROR : (int) REG_NOMATCH;
  5486. }
  5487.  
  5488.  
  5489. /* Returns a message corresponding to an error code, ERRCODE, returned
  5490.    from either regcomp or regexec.   We don't use PREG here.  */
  5491.  
  5492. size_t
  5493. regerror (errcode, preg, errbuf, errbuf_size)
  5494.     int errcode;
  5495.     const regex_t *preg;
  5496.     char *errbuf;
  5497.     size_t errbuf_size;
  5498. {
  5499.   const char *msg;
  5500.   size_t msg_size;
  5501.  
  5502.   if (errcode < 0
  5503.       || errcode >= (sizeof (re_error_msgid) / sizeof (re_error_msgid[0])))
  5504.     /* Only error codes returned by the rest of the code should be passed
  5505.        to this routine.  If we are given anything else, or if other regex
  5506.        code generates an invalid error code, then the program has a bug.
  5507.        Dump core so we can fix it.  */
  5508.     abort ();
  5509.  
  5510.   msg = gettext (re_error_msgid[errcode]);
  5511.  
  5512.   msg_size = strlen (msg) + 1; /* Includes the null.  */
  5513.  
  5514.   if (errbuf_size != 0)
  5515.     {
  5516.       if (msg_size > errbuf_size)
  5517.         {
  5518.           strncpy (errbuf, msg, errbuf_size - 1);
  5519.           errbuf[errbuf_size - 1] = 0;
  5520.         }
  5521.       else
  5522.         strcpy (errbuf, msg);
  5523.     }
  5524.  
  5525.   return msg_size;
  5526. }
  5527.  
  5528.  
  5529. /* Free dynamically allocated space used by PREG.  */
  5530.  
  5531. void
  5532. regfree (preg)
  5533.     regex_t *preg;
  5534. {
  5535.   if (preg->buffer != NULL)
  5536.     free (preg->buffer);
  5537.   preg->buffer = NULL;
  5538.  
  5539.   preg->allocated = 0;
  5540.   preg->used = 0;
  5541.  
  5542.   if (preg->fastmap != NULL)
  5543.     free (preg->fastmap);
  5544.   preg->fastmap = NULL;
  5545.   preg->fastmap_accurate = 0;
  5546.  
  5547.   if (preg->translate != NULL)
  5548.     free (preg->translate);
  5549.   preg->translate = NULL;
  5550. }
  5551.  
  5552. #endif /* not emacs  */
  5553.  
  5554. /*
  5555. Local variables:
  5556. make-backup-files: t
  5557. version-control: t
  5558. trim-versions-without-asking: nil
  5559. End:
  5560. */
  5561.